From bc8c21659a687eedb9e07f9d5928326896adab1b Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Thu, 19 Sep 2024 18:19:07 +0200 Subject: [PATCH 1/2] Be less strict with parameter lengths (#5225) * Refs #21537. Add regression test for interoperability issue. Signed-off-by: Miguel Company * Refs #21537. Add regression test for big parameters. Signed-off-by: Miguel Company * Refs #21537. Fix builtin deserialization length checks. Signed-off-by: Miguel Company * Refs #21537. Special cases in test. Signed-off-by: Miguel Company * Refs #21537. Extend test to full non-custom range. Signed-off-by: Miguel Company * Refs #21537. Extend parameter size to 64K. Signed-off-by: Miguel Company * Refs #21537. Uncrustify. Signed-off-by: Miguel Company * Refs #21638. Fix windows warning. Signed-off-by: Miguel Company --------- Signed-off-by: Miguel Company (cherry picked from commit 7b111f91119710b77f1d75bc772b976627e1e814) # Conflicts: # src/cpp/fastdds/core/policy/ParameterSerializer.hpp --- .../core/policy/ParameterSerializer.hpp | 89 ++- .../core/policy/QosPoliciesSerializer.hpp | 32 +- .../builtin/BuiltinDataSerializationTests.cpp | 508 ++++++++++++++++++ 3 files changed, 596 insertions(+), 33 deletions(-) diff --git a/src/cpp/fastdds/core/policy/ParameterSerializer.hpp b/src/cpp/fastdds/core/policy/ParameterSerializer.hpp index a63c1af43af..0469cd4fc93 100644 --- a/src/cpp/fastdds/core/policy/ParameterSerializer.hpp +++ b/src/cpp/fastdds/core/policy/ParameterSerializer.hpp @@ -251,7 +251,7 @@ inline bool ParameterSerializer::read_content_from_cdr_messa fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_LOCATOR_LENGTH) + if (parameter_length < PARAMETER_LOCATOR_LENGTH) { return false; } @@ -273,7 +273,7 @@ inline bool ParameterSerializer::read_content_from_cdr_message( fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_KEY_HASH_LENGTH) + if (parameter_length < PARAMETER_KEY_HASH_LENGTH) { return false; } @@ -343,7 +343,7 @@ inline bool ParameterSerializer::read_content_from_cdr_message( fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_PORT_LENGTH) + if (parameter_length < PARAMETER_PORT_LENGTH) { return false; } @@ -367,7 +367,7 @@ inline bool ParameterSerializer::read_content_from_cdr_message( fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_GUID_LENGTH) + if (parameter_length < PARAMETER_GUID_LENGTH) { return false; } @@ -394,7 +394,7 @@ inline bool ParameterSerializer::read_content_from_c fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_PROTOCOL_LENGTH) + if (parameter_length < PARAMETER_PROTOCOL_LENGTH) { return false; } @@ -422,7 +422,7 @@ inline bool ParameterSerializer::read_content_from_cdr_mess fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_VENDOR_LENGTH) + if (parameter_length < PARAMETER_VENDOR_LENGTH) { return false; } @@ -434,6 +434,61 @@ inline bool ParameterSerializer::read_content_from_cdr_mess } template<> +<<<<<<< HEAD +======= +inline bool ParameterSerializer::add_content_to_cdr_message( + const ParameterProductVersion_t& parameter, + rtps::CDRMessage_t* cdr_message) +{ + bool valid = rtps::CDRMessage::addOctet(cdr_message, parameter.version.major); + valid &= rtps::CDRMessage::addOctet(cdr_message, parameter.version.minor); + valid &= rtps::CDRMessage::addOctet(cdr_message, parameter.version.patch); + valid &= rtps::CDRMessage::addOctet(cdr_message, parameter.version.tweak); + return valid; +} + +template<> +inline bool ParameterSerializer::read_content_from_cdr_message( + ParameterProductVersion_t& parameter, + rtps::CDRMessage_t* cdr_message, + const uint16_t parameter_length) +{ + if (parameter_length < PARAMETER_PRODUCT_VERSION_LENGTH) + { + return false; + } + parameter.length = parameter_length; + bool valid = rtps::CDRMessage::readOctet(cdr_message, ¶meter.version.major); + valid &= rtps::CDRMessage::readOctet(cdr_message, ¶meter.version.minor); + valid &= rtps::CDRMessage::readOctet(cdr_message, ¶meter.version.patch); + valid &= rtps::CDRMessage::readOctet(cdr_message, ¶meter.version.tweak); + return valid; +} + +template<> +inline bool ParameterSerializer::add_content_to_cdr_message( + const ParameterDomainId_t& parameter, + fastdds::rtps::CDRMessage_t* cdr_message) +{ + return fastdds::rtps::CDRMessage::addUInt32(cdr_message, parameter.domain_id); +} + +template<> +inline bool ParameterSerializer::read_content_from_cdr_message( + ParameterDomainId_t& parameter, + fastdds::rtps::CDRMessage_t* cdr_message, + const uint16_t parameter_length) +{ + if (parameter_length < PARAMETER_DOMAINID_LENGTH) + { + return false; + } + parameter.length = parameter_length; + return fastdds::rtps::CDRMessage::readUInt32(cdr_message, ¶meter.domain_id); +} + +template<> +>>>>>>> 7b111f911 (Be less strict with parameter lengths (#5225)) inline bool ParameterSerializer::add_content_to_cdr_message( const ParameterIP4Address_t& parameter, fastrtps::rtps::CDRMessage_t* cdr_message) @@ -447,7 +502,7 @@ inline bool ParameterSerializer::read_content_from_cdr_me fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_IP4_LENGTH) + if (parameter_length < PARAMETER_IP4_LENGTH) { return false; } @@ -473,7 +528,7 @@ inline bool ParameterSerializer::read_content_from_cdr_message( fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_BOOL_LENGTH) + if (parameter_length < PARAMETER_BOOL_LENGTH) { return false; } @@ -501,7 +556,7 @@ inline bool ParameterSerializer::read_content_from_cdr_me fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_STATUS_INFO_LENGTH) + if (parameter_length < PARAMETER_STATUS_INFO_LENGTH) { return false; } @@ -530,7 +585,7 @@ inline bool ParameterSerializer::read_content_from_cdr_message fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_COUNT_LENGTH) + if (parameter_length < PARAMETER_COUNT_LENGTH) { return false; } @@ -552,7 +607,7 @@ inline bool ParameterSerializer::read_content_from_cdr_mess fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_ENTITYID_LENGTH) + if (parameter_length < PARAMETER_ENTITYID_LENGTH) { return false; } @@ -576,7 +631,7 @@ inline bool ParameterSerializer::read_content_from_cdr_message( fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_TIME_LENGTH) + if (parameter_length < PARAMETER_TIME_LENGTH) { return false; } @@ -604,7 +659,7 @@ inline bool ParameterSerializer::read_content_fro fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_BUILTINENDPOINTSET_LENGTH) + if (parameter_length < PARAMETER_BUILTINENDPOINTSET_LENGTH) { return false; } @@ -626,7 +681,7 @@ inline bool ParameterSerializer::read_content_from_ fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_NETWORKCONFIGSET_LENGTH) + if (parameter_length < PARAMETER_NETWORKCONFIGSET_LENGTH) { return false; } @@ -783,7 +838,7 @@ inline bool ParameterSerializer::read_content_from_cd fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_SAMPLEIDENTITY_LENGTH) + if (parameter_length < PARAMETER_SAMPLEIDENTITY_LENGTH) { return false; } @@ -1061,7 +1116,7 @@ inline bool ParameterSerializer::read_conten fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_PARTICIPANT_SECURITY_INFO_LENGTH) + if (parameter_length < PARAMETER_PARTICIPANT_SECURITY_INFO_LENGTH) { return false; } @@ -1087,7 +1142,7 @@ inline bool ParameterSerializer::read_content_f fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_ENDPOINT_SECURITY_INFO_LENGTH) + if (parameter_length < PARAMETER_ENDPOINT_SECURITY_INFO_LENGTH) { return false; } diff --git a/src/cpp/fastdds/core/policy/QosPoliciesSerializer.hpp b/src/cpp/fastdds/core/policy/QosPoliciesSerializer.hpp index cafef98e48a..ae55c2590bf 100644 --- a/src/cpp/fastdds/core/policy/QosPoliciesSerializer.hpp +++ b/src/cpp/fastdds/core/policy/QosPoliciesSerializer.hpp @@ -97,7 +97,7 @@ inline bool QosPoliciesSerializer::read_content_from_cdr_me fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_KIND_LENGTH) + if (parameter_length < PARAMETER_KIND_LENGTH) { return false; } @@ -126,7 +126,7 @@ inline bool QosPoliciesSerializer::read_content_from_cdr_mess fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_TIME_LENGTH) + if (parameter_length < PARAMETER_TIME_LENGTH) { return false; } @@ -155,7 +155,7 @@ inline bool QosPoliciesSerializer::read_content_from_cdr fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_TIME_LENGTH) + if (parameter_length < PARAMETER_TIME_LENGTH) { return false; } @@ -190,7 +190,7 @@ inline bool QosPoliciesSerializer::read_content_from_cdr_me fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_KIND_LENGTH + PARAMETER_TIME_LENGTH) + if (parameter_length < PARAMETER_KIND_LENGTH + PARAMETER_TIME_LENGTH) { return false; } @@ -229,7 +229,7 @@ inline bool QosPoliciesSerializer::read_content_from_cdr_m fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_KIND_LENGTH + PARAMETER_TIME_LENGTH) + if (parameter_length < PARAMETER_KIND_LENGTH + PARAMETER_TIME_LENGTH) { return false; } @@ -263,7 +263,7 @@ inline bool QosPoliciesSerializer::read_content_from_cdr_mes fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_KIND_LENGTH) + if (parameter_length < PARAMETER_KIND_LENGTH) { return false; } @@ -292,7 +292,7 @@ inline bool QosPoliciesSerializer::read_content_from_ fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_KIND_LENGTH) + if (parameter_length < PARAMETER_KIND_LENGTH) { return false; } @@ -323,7 +323,7 @@ inline bool QosPoliciesSerializer::read_content_from_cd fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != 12) + if (parameter_length < 12) { return false; } @@ -357,7 +357,7 @@ inline bool QosPoliciesSerializer::read_content_from_c fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_TIME_LENGTH) + if (parameter_length < PARAMETER_TIME_LENGTH) { return false; } @@ -397,7 +397,7 @@ inline bool QosPoliciesSerializer::read_content_from_cdr_ fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_PRESENTATION_LENGTH) + if (parameter_length < PARAMETER_PRESENTATION_LENGTH) { return false; } @@ -516,7 +516,7 @@ inline bool QosPoliciesSerializer::read_content_from_cdr_messa fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_KIND_LENGTH + 4) + if (parameter_length < PARAMETER_KIND_LENGTH + 4) { return false; } @@ -553,7 +553,7 @@ inline bool QosPoliciesSerializer::read_content_from fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_TIME_LENGTH + PARAMETER_KIND_LENGTH + 16) + if (parameter_length < PARAMETER_TIME_LENGTH + PARAMETER_KIND_LENGTH + 16) { return false; } @@ -588,7 +588,7 @@ inline bool QosPoliciesSerializer::read_content_from_cdr_mess fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_TIME_LENGTH) + if (parameter_length < PARAMETER_TIME_LENGTH) { return false; } @@ -615,7 +615,7 @@ inline bool QosPoliciesSerializer::read_content_from fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != 4) + if (parameter_length < 4) { return false; } @@ -638,7 +638,7 @@ inline bool QosPoliciesSerializer::read_content_from fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != 4) + if (parameter_length < 4) { return false; } @@ -800,7 +800,7 @@ inline bool QosPoliciesSerializer::read_content_fr fastrtps::rtps::CDRMessage_t* cdr_message, const uint16_t parameter_length) { - if (parameter_length != PARAMETER_BOOL_LENGTH) + if (parameter_length < PARAMETER_BOOL_LENGTH) { return false; } diff --git a/test/unittest/rtps/builtin/BuiltinDataSerializationTests.cpp b/test/unittest/rtps/builtin/BuiltinDataSerializationTests.cpp index e1a24da2b49..7a663735b1f 100644 --- a/test/unittest/rtps/builtin/BuiltinDataSerializationTests.cpp +++ b/test/unittest/rtps/builtin/BuiltinDataSerializationTests.cpp @@ -12,7 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include +#include +#include #include #include @@ -1856,6 +1859,511 @@ TEST(BuiltinDataSerializationTests, null_checks) } } +/*! + * This is a regression test for redmine issue #21537 + * + * It checks that Fast DDS can deserialize DATA(p), DATA(w), and DATA(r) messages captured from InterCom DDS. + */ +TEST(BuiltinDataSerializationTests, interoperability_with_intercomdds) +{ + const VendorId_t intercom_vendor_id = { 1, 5 }; + + // DATA(p) + { + // This was captured with wireshark from intercom_dds-3.16.2.0_shape_main_linux taken from + // https://github.com/omg-dds/dds-rtps/releases/tag/v1.2.2024 + octet data_p_buffer[] = + { + // Encapsulation + 0x00, 0x03, 0x00, 0x00, + // Participant GUID + 0x50, 0x00, 0x10, 0x00, + 0x01, 0x05, 0xfa, 0xd5, 0x7a, 0x09, 0x6a, 0x22, 0x84, 0xfb, 0x23, 0xa2, 0x00, 0x00, 0x01, 0xc1, + // Custom (0x8005) + 0x05, 0x80, 0x18, 0x00, + 0x12, 0x00, 0x00, 0x00, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x43, 0x4f, 0x4d, 0x20, 0x33, 0x5f, 0x31, + 0x36, 0x5f, 0x32, 0x5f, 0x30, 0x00, 0x00, 0x00, + // Custom (0x8006) + 0x06, 0x80, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, + // Custom (0x8007) + 0x07, 0x80, 0x0c, 0x00, + 0x01, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, + // Custom (0x8008) + 0x08, 0x80, 0x08, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // Custom (0x8009) + 0x09, 0x80, 0x34, 0x00, + 0x2e, 0x00, 0x00, 0x00, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x63, 0x6f, 0x6d, 0x5f, 0x64, 0x64, 0x73, + 0x2d, 0x33, 0x2e, 0x31, 0x36, 0x2e, 0x32, 0x2e, 0x30, 0x5f, 0x73, 0x68, 0x61, 0x70, 0x65, 0x5f, + 0x6d, 0x61, 0x69, 0x6e, 0x5f, 0x6c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x28, 0x37, 0x30, 0x34, 0x35, + 0x29, 0x00, 0x00, 0x00, + // Domain ID + 0x0f, 0x00, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, + // Builtin endpoint QoS + 0x77, 0x00, 0x04, 0x00, + 0x01, 0x00, 0x00, 0x00, + // Builtin endpoint set + 0x58, 0x00, 0x04, 0x00, + 0x3f, 0xfc, 0x00, 0x00, + // Protocol version + 0x15, 0x00, 0x04, 0x00, + 0x02, 0x05, 0x00, 0x00, + // Vendor ID + 0x16, 0x00, 0x04, 0x00, + 0x01, 0x05, 0x00, 0x00, + // Default Unicast Locator + 0x31, 0x00, 0x18, 0x00, + 0x01, 0x00, 0x00, 0x00, 0xf5, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xac, 0x1a, 0x9f, 0xd5, + // Default Multicast Locator + 0x48, 0x00, 0x18, 0x00, + 0x01, 0x00, 0x00, 0x00, 0xe9, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xef, 0xff, 0x00, 0x01, + // Metatraffic unicast locator + 0x32, 0x00, 0x18, 0x00, + 0x01, 0x00, 0x00, 0x00, 0xf4, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xac, 0x1a, 0x9f, 0xd5, + // Metatraffic multicast locator + 0x33, 0x00, 0x18, 0x00, + 0x01, 0x00, 0x00, 0x00, 0xe8, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xef, 0xff, 0x00, 0x01, + // Participant lease duration + 0x02, 0x00, 0x08, 0x00, + 0x2c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // Sentinel + 0x01, 0x00, 0x00, 0x00 + }; + + CDRMessage_t msg(0); + msg.init(data_p_buffer, static_cast(sizeof(data_p_buffer))); + msg.length = msg.max_size; + + ParticipantProxyData out({}); + EXPECT_NO_THROW(EXPECT_TRUE(out.readFromCDRMessage(&msg, true, network, false, intercom_vendor_id))); + } + + // DATA(w) + { + // This was captured with wireshark from intercom_dds-3.16.2.0_shape_main_linux taken from + // https://github.com/omg-dds/dds-rtps/releases/tag/v1.2.2024 + octet data_w_buffer[] = + { + // Encapsulation + 0x00, 0x03, 0x00, 0x00, + // Writer GUID + 0x5a, 0x00, 0x10, 0x00, + 0x01, 0x05, 0xfa, 0xd5, 0x7a, 0x09, 0x6a, 0x22, 0x84, 0xfb, 0x23, 0xa2, 0x00, 0x00, 0x01, 0x02, + // Participant GUID + 0x50, 0x00, 0x10, 0x00, + 0x01, 0x05, 0xfa, 0xd5, 0x7a, 0x09, 0x6a, 0x22, 0x84, 0xfb, 0x23, 0xa2, 0x00, 0x00, 0x01, 0xc1, + // Topic name + 0x05, 0x00, 0x0c, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x53, 0x71, 0x75, 0x61, 0x72, 0x65, 0x00, 0x00, + // Type name + 0x07, 0x00, 0x10, 0x00, + 0x0a, 0x00, 0x00, 0x00, 0x53, 0x68, 0x61, 0x70, 0x65, 0x54, 0x79, 0x70, 0x65, 0x00, 0x00, 0x00, + // Type information + 0x75, 0x00, 0x5c, 0x00, + 0x58, 0x00, 0x00, 0x00, 0x01, 0x10, 0x00, 0x50, 0x24, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, + 0xf1, 0xa5, 0x12, 0xf3, 0x95, 0xe2, 0xba, 0xb0, 0xb9, 0xfc, 0x83, 0x8e, 0x08, 0x6e, 0x2c, 0x00, + 0x57, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x10, 0x00, 0x50, 0x24, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0xf2, 0x77, 0x32, 0x07, + 0xfb, 0x72, 0x38, 0x6e, 0x0d, 0xdb, 0x0e, 0x1a, 0x2b, 0x4f, 0xbe, 0x00, 0x84, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // Topic data + 0x2e, 0x00, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, + // Data representation + 0x73, 0x00, 0x08, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // Property list + 0x59, 0x00, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // Data tags + 0x03, 0x10, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, + // Service instance name + 0x80, 0x00, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, + // Related entity GUID + 0x81, 0x00, 0x10, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // History + 0x40, 0x00, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + // Custom (0x8030) + 0x30, 0x80, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, + // Sentinel + 0x01, 0x00, 0x00, 0x00 + }; + + CDRMessage_t msg(0); + msg.init(data_w_buffer, static_cast(sizeof(data_w_buffer))); + msg.length = msg.max_size; + + WriterProxyData out(max_unicast_locators, max_multicast_locators); + EXPECT_NO_THROW(EXPECT_TRUE(out.readFromCDRMessage(&msg, network, true, intercom_vendor_id))); + } + + // DATA(r) + { + // This was captured with wireshark from intercom_dds-3.16.2.0_shape_main_linux taken from + // https://github.com/omg-dds/dds-rtps/releases/tag/v1.2.2024 + uint8_t data_r_buffer[] = + { + // Encapsulation + 0x00, 0x03, 0x00, 0x00, + // Reader GUID + 0x5a, 0x00, 0x10, 0x00, + 0x01, 0x05, 0x0f, 0xda, 0x14, 0xdd, 0x32, 0x62, 0x74, 0xef, 0x08, 0xeb, 0x00, 0x00, 0x01, 0x07, + // Participant GUID + 0x50, 0x00, 0x10, 0x00, + 0x01, 0x05, 0x0f, 0xda, 0x14, 0xdd, 0x32, 0x62, 0x74, 0xef, 0x08, 0xeb, 0x00, 0x00, 0x01, 0xc1, + // Topic name + 0x05, 0x00, 0x0c, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x53, 0x71, 0x75, 0x61, 0x72, 0x65, 0x00, 0x00, + // Type name + 0x07, 0x00, 0x10, 0x00, + 0x0a, 0x00, 0x00, 0x00, 0x53, 0x68, 0x61, 0x70, 0x65, 0x54, 0x79, 0x70, 0x65, 0x00, 0x00, 0x00, + // Type information + 0x75, 0x00, 0x5c, 0x00, + 0x58, 0x00, 0x00, 0x00, 0x01, 0x10, 0x00, 0x50, 0x24, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, + 0xf1, 0xa5, 0x12, 0xf3, 0x95, 0xe2, 0xba, 0xb0, 0xb9, 0xfc, 0x83, 0x8e, 0x08, 0x6e, 0x2c, 0x00, + 0x57, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x10, 0x00, 0x50, 0x24, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0xf2, 0x77, 0x32, 0x07, + 0xfb, 0x72, 0x38, 0x6e, 0x0d, 0xdb, 0x0e, 0x1a, 0x2b, 0x4f, 0xbe, 0x00, 0x84, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // Reliability + 0x1a, 0x00, 0x10, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9a, 0x99, 0x99, 0x19, 0x00, 0x00, 0x00, 0x00, + // Topic data + 0x2e, 0x00, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, + // Data representation + 0x73, 0x00, 0x08, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // Type consistency + 0x74, 0x00, 0x08, 0x00, + 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, + // Property list + 0x59, 0x00, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // Data tags + 0x03, 0x10, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, + // Service instance name + 0x80, 0x00, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, + // Related entity GUID + 0x81, 0x00, 0x10, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // History + 0x40, 0x00, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + // Sentinel + 0x01, 0x00, 0x00, 0x00 + }; + + CDRMessage_t msg(0); + msg.init(data_r_buffer, static_cast(sizeof(data_r_buffer))); + msg.length = msg.max_size; + + ReaderProxyData out(max_unicast_locators, max_multicast_locators); + EXPECT_NO_THROW(EXPECT_TRUE(out.readFromCDRMessage(&msg, network, true, intercom_vendor_id))); + } +} + +/*! + * This is a regression test for redmine issue #21537 + * + * It checks deserialization of builtin data with big parameters. + */ +TEST(BuiltinDataSerializationTests, deserialization_of_big_parameters) +{ + constexpr size_t encapsulation_length = 4; + constexpr size_t guid_length = 20; + constexpr size_t topic_name_length = 16; + constexpr size_t type_name_length = 16; + constexpr size_t parameter_length = 65536; + constexpr size_t sentinel_length = 4; + constexpr size_t total_length = + encapsulation_length + // encapsulation + parameter_length + // Big parameter + guid_length + // Participant GUID + guid_length + // Endpoint GUID + topic_name_length + // Topic name + type_name_length + // Type name + sentinel_length; // Sentinel + std::array buffer{{0}}; + + // Encapsulation (PL_CDR_LE) + size_t pos = 0; + buffer[pos++] = 0x00; + buffer[pos++] = 0x03; + buffer[pos++] = 0x00; + buffer[pos++] = 0x00; + + // Room for the big parameter + pos += parameter_length; + + // Participant GUID + buffer[pos++] = 0x50; + buffer[pos++] = 0x00; + buffer[pos++] = 0x10; + buffer[pos++] = 0x00; + buffer[pos++] = 0x01; + buffer[pos++] = 0x05; + buffer[pos++] = 0x0f; + buffer[pos++] = 0xda; + buffer[pos++] = 0x14; + buffer[pos++] = 0xdd; + buffer[pos++] = 0x32; + buffer[pos++] = 0x62; + buffer[pos++] = 0x74; + buffer[pos++] = 0xef; + buffer[pos++] = 0x08; + buffer[pos++] = 0xeb; + buffer[pos++] = 0x00; + buffer[pos++] = 0x00; + buffer[pos++] = 0x01; + buffer[pos++] = 0xc1; + + // Endpoint GUID + buffer[pos++] = 0x5a; + buffer[pos++] = 0x00; + buffer[pos++] = 0x10; + buffer[pos++] = 0x00; + buffer[pos++] = 0x01; + buffer[pos++] = 0x05; + buffer[pos++] = 0x0f; + buffer[pos++] = 0xda; + buffer[pos++] = 0x14; + buffer[pos++] = 0xdd; + buffer[pos++] = 0x32; + buffer[pos++] = 0x62; + buffer[pos++] = 0x74; + buffer[pos++] = 0xef; + buffer[pos++] = 0x08; + buffer[pos++] = 0xeb; + buffer[pos++] = 0x00; + buffer[pos++] = 0x00; + buffer[pos++] = 0x01; + buffer[pos++] = 0x07; + + // Topic name ("Square") + buffer[pos++] = 0x05; + buffer[pos++] = 0x00; + buffer[pos++] = 0x0c; + buffer[pos++] = 0x00; + buffer[pos++] = 0x07; + buffer[pos++] = 0x00; + buffer[pos++] = 0x00; + buffer[pos++] = 0x00; + buffer[pos++] = 0x53; + buffer[pos++] = 0x71; + buffer[pos++] = 0x75; + buffer[pos++] = 0x61; + buffer[pos++] = 0x72; + buffer[pos++] = 0x65; + buffer[pos++] = 0x00; + buffer[pos++] = 0x00; + + // Type name ("MyType") + buffer[pos++] = 0x07; + buffer[pos++] = 0x00; + buffer[pos++] = 0x0c; + buffer[pos++] = 0x00; + buffer[pos++] = 0x07; + buffer[pos++] = 0x00; + buffer[pos++] = 0x00; + buffer[pos++] = 0x00; + buffer[pos++] = 0x4d; + buffer[pos++] = 0x79; + buffer[pos++] = 0x54; + buffer[pos++] = 0x79; + buffer[pos++] = 0x70; + buffer[pos++] = 0x65; + buffer[pos++] = 0x00; + buffer[pos++] = 0x00; + + // Sentinel + buffer[pos++] = 0x01; + buffer[pos++] = 0x00; + buffer[pos++] = 0x00; + buffer[pos++] = 0x00; + + ASSERT_EQ(total_length, pos); + + std::set failed_for_data_p; + std::set failed_for_data_w; + std::set failed_for_data_r; + + // Loop all parameter IDs in the standard range + for (uint16_t pid = 0x2; pid <= 0x7FFF; ++pid) + { + // Clear big parameter + octet zero = 0u; + auto param_begin = buffer.begin() + encapsulation_length; + std::fill(param_begin, param_begin + parameter_length, zero); + + // Set the parameter ID of the big parameter + constexpr uint16_t big_parameter_plength = parameter_length - 4; + buffer[encapsulation_length] = static_cast(pid & 0xFF); + buffer[encapsulation_length + 1] = static_cast((pid >> 8) & 0xFF); + buffer[encapsulation_length + 2] = static_cast(big_parameter_plength & 0xFF); + buffer[encapsulation_length + 3] = static_cast((big_parameter_plength >> 8) & 0xFF); + + // Beware of semantically incorrect parameters + switch (pid) + { + // The protocol version should be 2 + case eprosima::fastdds::dds::PID_PROTOCOL_VERSION: + { + buffer[encapsulation_length + 4] = 0x02; + } + break; + + // The length of some string parameters should be lower than 256 + case eprosima::fastdds::dds::PID_ENTITY_NAME: + case eprosima::fastdds::dds::PID_TYPE_NAME: + case eprosima::fastdds::dds::PID_TOPIC_NAME: + { + buffer[encapsulation_length + 2] = 0xFF; + buffer[encapsulation_length + 3] = 0x00; + } + break; + + // Data parameters should fill the whole parameter + case eprosima::fastdds::dds::PID_USER_DATA: + case eprosima::fastdds::dds::PID_TOPIC_DATA: + case eprosima::fastdds::dds::PID_GROUP_DATA: + { + constexpr uint16_t inner_data_length = big_parameter_plength - 4; + buffer[encapsulation_length + 4] = static_cast(inner_data_length & 0xFF); + buffer[encapsulation_length + 5] = static_cast((inner_data_length >> 8) & 0xFF); + } + break; + + // Custom content for partition + case eprosima::fastdds::dds::PID_PARTITION: + { + // Number of partitions (1) + buffer[encapsulation_length + 4] = 0x01; + buffer[encapsulation_length + 5] = 0x00; + buffer[encapsulation_length + 6] = 0x00; + buffer[encapsulation_length + 7] = 0x00; + // Partition name length (fills the rest of the parameter) + constexpr uint16_t partition_length = big_parameter_plength - 4 - 4; + buffer[encapsulation_length + 8] = static_cast(partition_length & 0xFF); + buffer[encapsulation_length + 9] = static_cast((partition_length >> 8) & 0xFF); + buffer[encapsulation_length + 10] = 0x00; + buffer[encapsulation_length + 11] = 0x00; + } + break; + + // Custom content for security tokens + case eprosima::fastdds::dds::PID_IDENTITY_TOKEN: + case eprosima::fastdds::dds::PID_PERMISSIONS_TOKEN: + { + // Content is a string + properties (0) + binary properties (0) + // Should fill the whole parameter + constexpr uint16_t token_string_length = big_parameter_plength - 4 - 4 - 4; + buffer[encapsulation_length + 4] = static_cast(token_string_length & 0xFF); + buffer[encapsulation_length + 5] = static_cast((token_string_length >> 8) & 0xFF); + } + break; + } + + // Deserialize a DATA(p) + { + CDRMessage_t msg(0); + msg.init(buffer.data(), static_cast(buffer.size())); + msg.length = msg.max_size; + + RTPSParticipantAllocationAttributes att; + att.data_limits.max_user_data = parameter_length; + ParticipantProxyData out({}); + EXPECT_NO_THROW( + if (!out.readFromCDRMessage(&msg, true, network, false)) + { + failed_for_data_p.insert(pid); + } + ); + } + + // Deserialize a DATA(w) + { + CDRMessage_t msg(0); + msg.init(buffer.data(), static_cast(buffer.size())); + msg.length = msg.max_size; + + VariableLengthDataLimits limits; + limits.max_user_data = parameter_length; + WriterProxyData out(max_unicast_locators, max_multicast_locators, limits); + EXPECT_NO_THROW( + if (!out.readFromCDRMessage(&msg, network, true)) + { + failed_for_data_w.insert(pid); + } + ); + } + + // Deserialize a DATA(r) + { + CDRMessage_t msg(0); + msg.init(buffer.data(), static_cast(buffer.size())); + msg.length = msg.max_size; + + VariableLengthDataLimits limits; + limits.max_user_data = parameter_length; + ReaderProxyData out(max_unicast_locators, max_multicast_locators, limits); + EXPECT_NO_THROW( + if (!out.readFromCDRMessage(&msg, network, true)) + { + failed_for_data_r.insert(pid); + } + ); + } + } + + // Check if any parameter ID failed + EXPECT_EQ(failed_for_data_p.size(), 0u); + EXPECT_EQ(failed_for_data_w.size(), 0u); + EXPECT_EQ(failed_for_data_r.size(), 0u); + + // Print the failed parameter IDs + if (!failed_for_data_p.empty()) + { + std::cout << "Failed for DATA(p): "; + for (uint16_t pid : failed_for_data_p) + { + std::cout << std::hex << std::setfill('0') << std::setw(4) << pid << " "; + } + std::cout << std::endl; + } + if (!failed_for_data_w.empty()) + { + std::cout << "Failed for DATA(w): "; + for (uint16_t pid : failed_for_data_w) + { + std::cout << std::hex << std::setfill('0') << std::setw(4) << pid << " "; + } + std::cout << std::endl; + } + if (!failed_for_data_r.empty()) + { + std::cout << "Failed for DATA(r): "; + for (uint16_t pid : failed_for_data_r) + { + std::cout << std::hex << std::setfill('0') << std::setw(4) << pid << " "; + } + std::cout << std::endl; + } +} + } // namespace rtps } // namespace fastrtps } // namespace eprosima From 9e8cf7e9bf5ab051333ce13c71ca553f5a476ec4 Mon Sep 17 00:00:00 2001 From: JesusPoderoso Date: Mon, 23 Sep 2024 11:02:16 +0200 Subject: [PATCH 2/2] Refs #21638: Fix conflicts Signed-off-by: JesusPoderoso --- .../core/policy/ParameterSerializer.hpp | 55 ------------------- 1 file changed, 55 deletions(-) diff --git a/src/cpp/fastdds/core/policy/ParameterSerializer.hpp b/src/cpp/fastdds/core/policy/ParameterSerializer.hpp index 0469cd4fc93..0eb4c094e9a 100644 --- a/src/cpp/fastdds/core/policy/ParameterSerializer.hpp +++ b/src/cpp/fastdds/core/policy/ParameterSerializer.hpp @@ -434,61 +434,6 @@ inline bool ParameterSerializer::read_content_from_cdr_mess } template<> -<<<<<<< HEAD -======= -inline bool ParameterSerializer::add_content_to_cdr_message( - const ParameterProductVersion_t& parameter, - rtps::CDRMessage_t* cdr_message) -{ - bool valid = rtps::CDRMessage::addOctet(cdr_message, parameter.version.major); - valid &= rtps::CDRMessage::addOctet(cdr_message, parameter.version.minor); - valid &= rtps::CDRMessage::addOctet(cdr_message, parameter.version.patch); - valid &= rtps::CDRMessage::addOctet(cdr_message, parameter.version.tweak); - return valid; -} - -template<> -inline bool ParameterSerializer::read_content_from_cdr_message( - ParameterProductVersion_t& parameter, - rtps::CDRMessage_t* cdr_message, - const uint16_t parameter_length) -{ - if (parameter_length < PARAMETER_PRODUCT_VERSION_LENGTH) - { - return false; - } - parameter.length = parameter_length; - bool valid = rtps::CDRMessage::readOctet(cdr_message, ¶meter.version.major); - valid &= rtps::CDRMessage::readOctet(cdr_message, ¶meter.version.minor); - valid &= rtps::CDRMessage::readOctet(cdr_message, ¶meter.version.patch); - valid &= rtps::CDRMessage::readOctet(cdr_message, ¶meter.version.tweak); - return valid; -} - -template<> -inline bool ParameterSerializer::add_content_to_cdr_message( - const ParameterDomainId_t& parameter, - fastdds::rtps::CDRMessage_t* cdr_message) -{ - return fastdds::rtps::CDRMessage::addUInt32(cdr_message, parameter.domain_id); -} - -template<> -inline bool ParameterSerializer::read_content_from_cdr_message( - ParameterDomainId_t& parameter, - fastdds::rtps::CDRMessage_t* cdr_message, - const uint16_t parameter_length) -{ - if (parameter_length < PARAMETER_DOMAINID_LENGTH) - { - return false; - } - parameter.length = parameter_length; - return fastdds::rtps::CDRMessage::readUInt32(cdr_message, ¶meter.domain_id); -} - -template<> ->>>>>>> 7b111f911 (Be less strict with parameter lengths (#5225)) inline bool ParameterSerializer::add_content_to_cdr_message( const ParameterIP4Address_t& parameter, fastrtps::rtps::CDRMessage_t* cdr_message)