Fix: truncated len in lttng_event_rule_user_tracepoint_serialize()
authorMichael Jeanson <mjeanson@efficios.com>
Thu, 23 Mar 2023 16:45:18 +0000 (12:45 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 23 Mar 2023 19:04:26 +0000 (15:04 -0400)
Observed issue
==============

On 64-bit big-endian platforms, the serialization/deserialization tests
of tracepoint event rules fail since the length of individual exclusions
is truncated in lttng_event_rule_user_tracepoint_serialize and appear as
"0" on the receiving end.

Cause
=====

The length of the exclusion name string is stored in a variable of type
`size_t`. However, since the protocol expects it to be expressed as a
uint32_t, the value is added to the payload by copying the first 4 bytes
of the value.

On a 32-bit system this would be fine since `sizeof(size_t) == 4`. Even
worse, it would work most of the time (assuming an exclusion name string
< 4GiB) on a little-endian 64-bit system as the least significant bits
would be copied and correctly express the length of the string.

On a big-endian 64-bit platform, the most-significant 4 bytes are copied
to the payload buffer thus making the string length appear as "0".

Solution
========

A temporary variable is used to hold the "casted" value and make it safe
to copy to the payload buffer regardless of the platform's endianness.

Known drawbacks
===============

None.

Change-Id: I64c03345fff7ffea2f8fcb84692a085da31c421b
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/common/event-rule/user-tracepoint.cpp

index 55b1c2505c9fb76ace05fa3df06ded60facd3354..83c7c885c784e99868ef3e8c9c9f07aaf7554339 100644 (file)
@@ -149,6 +149,7 @@ static int lttng_event_rule_user_tracepoint_serialize(const struct lttng_event_r
 
        for (i = 0; i < exclusion_count; i++) {
                size_t len;
+               uint32_t serialized_len;
                const char *exclusion;
 
                status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
@@ -156,8 +157,10 @@ static int lttng_event_rule_user_tracepoint_serialize(const struct lttng_event_r
                LTTNG_ASSERT(status == LTTNG_EVENT_RULE_STATUS_OK);
 
                len = strlen(exclusion) + 1;
+
+               serialized_len = len;
                /* Append exclusion length, includes the null terminator. */
-               ret = lttng_dynamic_buffer_append(&payload->buffer, &len, sizeof(uint32_t));
+               ret = lttng_dynamic_buffer_append(&payload->buffer, &serialized_len, sizeof(serialized_len));
                if (ret) {
                        goto end;
                }
This page took 0.027431 seconds and 4 git commands to generate.