Fix: lttng-sessiond: event_context might leak
[lttng-tools.git] / src / bin / lttng-sessiond / client.cpp
index f28037dcbdf592fd6977dd4dbf0fc25b2f1a80b9..69a6497ee326d48bd64012e60697dbfb1fcbff90 100644 (file)
@@ -842,6 +842,7 @@ static enum lttng_error_code receive_lttng_event_context(
        ssize_t sock_recv_len;
        enum lttng_error_code ret_code;
        struct lttng_payload event_context_payload;
+       struct lttng_event_context *context = NULL;
 
        lttng_payload_init(&event_context_payload);
 
@@ -864,22 +865,33 @@ static enum lttng_error_code receive_lttng_event_context(
 
        /* Deserialize event. */
        {
+               ssize_t len;
                struct lttng_payload_view event_context_view =
                                lttng_payload_view_from_payload(
                                                &event_context_payload, 0, -1);
 
-               if (lttng_event_context_create_from_payload(
-                               &event_context_view, out_event_context) !=
-                               event_context_len) {
-                       ERR("Invalid event context received as part of command payload");
+               len = lttng_event_context_create_from_payload(
+                               &event_context_view, &context);
+
+               if (len < 0) {
+                       ERR("Failed to create a event context from the received buffer");
+                       ret_code = LTTNG_ERR_INVALID_PROTOCOL;
+                       goto end;
+               }
+
+               if (len != event_context_len) {
+                       ERR("Event context from the received buffer is not the advertised length: expected length = %zu, payload length = %zd", event_context_len, len);
                        ret_code = LTTNG_ERR_INVALID_PROTOCOL;
                        goto end;
                }
        }
 
+       *out_event_context = context;
+       context = NULL;
        ret_code = LTTNG_OK;
 
 end:
+       lttng_event_context_destroy(context);
        lttng_payload_reset(&event_context_payload);
        return ret_code;
 }
@@ -1353,7 +1365,7 @@ skip_domain:
        switch (cmd_ctx->lsm.cmd_type) {
        case LTTNG_ADD_CONTEXT:
        {
-               struct lttng_event_context *event_context;
+               struct lttng_event_context *event_context = NULL;
                const enum lttng_error_code ret_code =
                        receive_lttng_event_context(
                                cmd_ctx, *sock, sock_error, &event_context);
This page took 0.023524 seconds and 4 git commands to generate.