Skip to contentSkip to conversation

Fix: lttng-live: use-after-free in get_next_index()

PATCH New Mathieu Desnoyers 2 participants 7 messages Dec 5, 2019 – Dec 13, 2019 2 messages not in the archive Library code

lore
Share & tools
lore

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

#1 lore raw

PATCH 1/6 Fix: lttng-live: use-after-free in get_next_index()

Running babeltrace under valgrind with a test-cases doing per-pid lttng tracing in live mode triggers this use-after-free in get_next_index() when stream is hung up.

---
 formats/lttng-live/lttng-live-comm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
formats/lttng-live/lttng-live-comm.c +1 -1
index 33a78029..96817f5e 100644
@@ -1108,8 +1108,8 @@ retry:
 		viewer_stream->in_trace = 0;
 		bt_list_del(&viewer_stream->trace_stream_node);
 		bt_list_del(&viewer_stream->session_stream_node);
-		g_free(viewer_stream);
 		*stream_id = be64toh(rp->stream_id);
+		g_free(viewer_stream);
 		break;
 	case LTTNG_VIEWER_INDEX_ERR:
 		fprintf(stderr, "[error] get_next_index: error\n");
Signature · 1 line
2.17.1
#2 lore raw

PATCH 2/6 Fix: trace-collection: trace clock use after free

In reply to a message not in the archive

The trace collection should copy the trace clock object rather than take a reference to the first trace's trace clock, because it may be freed when the trace is removed (e.g. application going away in per-pid live tracing).

---
 lib/trace-collection.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
lib/trace-collection.c +18 -3
index 035d2dc2..8e4a1432 100644
@@ -76,7 +76,7 @@ static void clock_add(gpointer key, gpointer value, gpointer user_data)
 {
 	struct clock_match *clock_match = user_data;
 	GHashTable *tc_clocks = clock_match->clocks;
-	struct ctf_clock *t_clock = value;
+	struct ctf_clock *t_clock = value, *clock_copy;
 	GQuark v;
 
 	if (t_clock->absolute)
@@ -104,9 +104,14 @@ static void clock_add(gpointer key, gpointer value, gpointer user_data)
 				clock_match->tc->single_clock_offset_avg =
 					clock_match->tc->offset_first;
 			}
+			clock_copy = g_new0(struct ctf_clock, 1);
+			*clock_copy = *t_clock;
+			if (t_clock->description) {
+				clock_copy->description = g_strdup(t_clock->description);
+			}
 			g_hash_table_insert(tc_clocks,
 				(gpointer) (unsigned long) v,
-				value);
+				clock_copy);
 		} else if (!t_clock->absolute) {
 			int64_t diff_ns;
 
@@ -209,11 +214,21 @@ int bt_trace_collection_remove(struct trace_collection *tc,
 
 }
 
+static
+void clock_free(gpointer data)
+{
+	struct ctf_clock *clock = data;
+
+	g_free(clock->description);
+	g_free(clock);
+}
+
 void bt_init_trace_collection(struct trace_collection *tc)
 {
 	assert(tc);
 	tc->array = g_ptr_array_new();
-	tc->clocks = g_hash_table_new(g_direct_hash, g_direct_equal);
+	tc->clocks = g_hash_table_new_full(g_direct_hash, g_direct_equal,
+			NULL, clock_free);
 	tc->single_clock_offset_avg = 0;
 	tc->offset_first = 0;
 	tc->delta_offset_first_sum = 0;
Signature · 1 line
2.17.1
#3 lore raw

PATCH 3/6 Fix: lttng-live: lttng_live_open_trace_read memory leak

In reply to a message not in the archive
---
 formats/lttng-live/lttng-live-plugin.c | 1 +
 1 file changed, 1 insertion(+)
formats/lttng-live/lttng-live-plugin.c +1 -0
index ed52a995..0bebdd89 100644
@@ -288,6 +288,7 @@ static int lttng_live_open_trace_read(const char *path)
 	}
 
 end_free:
+	g_array_free(ctx->session_ids, TRUE);
 	g_hash_table_destroy(ctx->session->ctf_traces);
 	free_session_streams(ctx->session);
 	g_free(ctx->session);
Signature · 1 line
2.17.1
#4 lore raw

PATCH 4/6 Fix: lib/iterator.c: unbalanced ctx put (leak)

In reply to a message not in the archive

Missing context put in iterator init error path.

---
 lib/iterator.c | 3 +++
 1 file changed, 3 insertions(+)
lib/iterator.c +3 -0
index 639a2d29..77093217 100644
@@ -778,6 +778,8 @@ int bt_iter_init(struct bt_iter *iter,
 error:
 	bt_heap_free(iter->stream_heap);
 error_heap_init:
+	bt_context_put(ctx);
+	iter->ctx = NULL;
 	g_free(iter->stream_heap);
 	iter->stream_heap = NULL;
 error_ctx:
@@ -812,6 +814,7 @@ void bt_iter_fini(struct bt_iter *iter)
 	}
 	iter->ctx->current_iterator = NULL;
 	bt_context_put(iter->ctx);
+	iter->ctx = NULL;
 }
 
 void bt_iter_destroy(struct bt_iter *iter)
Signature · 1 line
2.17.1
#5 lore raw

PATCH 5/6 Fix: lttng-live: ctf_live_packet_seek stream hang up handling

In reply to a message not in the archive

When get_next_index sets the index position to EOF, ctf_live_packet_seek() should in turn set the stream position to EOF to propagate the hung up state.

---
 formats/lttng-live/lttng-live-comm.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
formats/lttng-live/lttng-live-comm.c +5 -1
index 96817f5e..484c025d 100644
@@ -1276,7 +1276,11 @@ retry:
 				cur_index->packet_size, cur_index->offset,
 				cur_index->content_size,
 				cur_index->ts_cycles.timestamp_end);
-
+		if (cur_index->offset == EOF) {
+			pos->offset = EOF;
+			ret = -BT_PACKET_SEEK_ERROR;
+			goto end;
+		}
 	}
 
 	/*
Signature · 1 line
2.17.1
#6 lore raw

PATCH 6/6 Fix: lttng-live format: do not error out on empty streams hang up

In reply to a message not in the archive

Attaching to a stream hung up before providing any trace packet causes ctf_open_mmap_stream_read() to return an error.

This kind of scenario can happen with the upcoming "lttng clear" feature.

---
 formats/ctf/ctf.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
formats/ctf/ctf.c +6 -1
index 980ebc9a..1ba9017f 100644
@@ -2571,8 +2571,13 @@ int ctf_open_mmap_stream_read(struct ctf_trace *td,
 	}
 
 	ret = prepare_mmap_stream_definition(td, file_stream, packet_seek);
-	if (ret)
+	if (ret) {
+		/* We need to check for EOF here for empty files. */
+		if (unlikely(file_stream->pos.offset == EOF)) {
+			ret = 0;
+		}
 		goto error_index;
+	}
 
 	/*
 	 * For now, only a single clock per trace is supported.
Signature · 1 line
2.17.1

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

#8 lore raw

Re: [PATCH babeltrace-1.5 6/6] Fix: lttng-live format: do not error out on empty streams hang up

All patches of this series were merged in stable-1.5.

Thanks!
Jérémie

Quoted 34 lines · Mathieu Desnoyers

Attaching to a stream hung up before providing any trace packet causes ctf_open_mmap_stream_read() to return an error.

This kind of scenario can happen with the upcoming "lttng clear" feature.

---
 formats/ctf/ctf.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
formats/ctf/ctf.c +6 -2
index 980ebc9a..1ba9017f 100644
@@ -2571,8 +2571,13 @@ int ctf_open_mmap_stream_read(struct ctf_trace *td,
        }
 
        ret = prepare_mmap_stream_definition(td, file_stream, packet_seek);
-       if (ret)
+       if (ret) {
+               /* We need to check for EOF here for empty files. */
+               if (unlikely(file_stream->pos.offset == EOF)) {
+                       ret = 0;
+               }
                goto error_index;
+       }
 
        /*
         * For now, only a single clock per trace is supported.
--

2.17.1

Signature · 7 lines
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev