Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[13295] Fix memory leak in SecurityManager #2483

Merged
merged 6 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/cpp/rtps/security/SecurityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,12 @@ void SecurityManager::remove_discovered_participant_info(

authentication_plugin_->return_identity_handle(auth_ptr->identity_handle_, exception);
auth_ptr->identity_handle_ = nullptr;

if (auth_ptr->change_sequence_number_ != SequenceNumber_t::unknown())
{
participant_stateless_message_writer_history_->remove_change(auth_ptr->change_sequence_number_);
auth_ptr->change_sequence_number_ = SequenceNumber_t::unknown();
}
}
}

Expand Down
39 changes: 39 additions & 0 deletions test/blackbox/api/fastrtps_deprecated/PubSubReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,45 @@ class PubSubReader
std::cout << "Reader discovery finished..." << std::endl;
}

bool wait_participant_discovery(
unsigned int min_participants = 1,
std::chrono::seconds timeout = std::chrono::seconds::zero())
{
bool ret_value = true;
std::unique_lock<std::mutex> lock(mutexDiscovery_);

std::cout << "Reader is waiting discovery of at least " << min_participants << " participants..." << std::endl;

if (timeout == std::chrono::seconds::zero())
{
cvDiscovery_.wait(lock, [&]()
{
return participant_matched_ >= min_participants;
});
}
else
{
if (!cvDiscovery_.wait_for(lock, timeout, [&]()
{
return participant_matched_ >= min_participants;
}))
{
ret_value = false;
}
}

if (ret_value)
{
std::cout << "Reader participant discovery finished successfully..." << std::endl;
}
else
{
std::cout << "Reader participant discovery finished unsuccessfully..." << std::endl;
}

return ret_value;
}

bool wait_participant_undiscovery(
std::chrono::seconds timeout = std::chrono::seconds::zero())
{
Expand Down
79 changes: 79 additions & 0 deletions test/blackbox/common/BlackboxTestsSecurity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h>
#include <rtps/transport/test_UDPv4Transport.h>

#include <fastdds/dds/log/Log.hpp>

using namespace eprosima::fastrtps;
using namespace eprosima::fastrtps::rtps;
using test_UDPv4Transport = eprosima::fastdds::rtps::test_UDPv4Transport;
Expand Down Expand Up @@ -438,6 +440,83 @@ TEST_P(Security, BuiltinAuthenticationPlugin_PKIDH_lossy_conditions)
reader.wait_discovery();
}

// Regresion test for Refs #13295, github #2362
TEST_P(Security, BuiltinAuthenticationPlugin_second_participant_creation_loop)
{
constexpr size_t n_loops = 101;

using Log = eprosima::fastdds::dds::Log;
using LogConsumer = eprosima::fastdds::dds::LogConsumer;

// A LogConsumer that just counts the number of entries consumed
struct TestConsumer : public LogConsumer
{
TestConsumer(
std::atomic_size_t& n_logs_ref)
: n_logs_(n_logs_ref)
{
}

void Consume(
const Log::Entry&) override
{
++n_logs_;
}

private:

std::atomic_size_t& n_logs_;
};

// Counter for log entries
std::atomic<size_t>n_logs{};

// Prepare Log module to check that no SECURITY errors are produced
Log::SetCategoryFilter(std::regex("SECURITY"));
Log::SetVerbosity(Log::Kind::Error);
Log::ClearConsumers();
Log::RegisterConsumer(std::unique_ptr<LogConsumer>(new TestConsumer(n_logs)));

// Prepare participant properties
PropertyPolicy property_policy;
property_policy.properties().emplace_back(Property("dds.sec.auth.plugin", "builtin.PKI-DH"));
property_policy.properties().emplace_back(Property("dds.sec.auth.builtin.PKI-DH.identity_ca",
"file://" + std::string(certs_path) + "/maincacert.pem"));
property_policy.properties().emplace_back(Property("dds.sec.auth.builtin.PKI-DH.identity_certificate",
"file://" + std::string(certs_path) + "/mainpubcert.pem"));
property_policy.properties().emplace_back(Property("dds.sec.auth.builtin.PKI-DH.private_key",
"file://" + std::string(certs_path) + "/mainpubkey.pem"));

// Create the participant being checked
PubSubReader<HelloWorldPubSubType> main_participant("HelloWorldTopic");
main_participant.property_policy(property_policy).init();
EXPECT_TRUE(main_participant.isInitialized());

// Perform a loop in which we create another participant, and destroy it just after it has been discovered.
// This is the best reproducer of the issue, as authentication messages should be sent when a remote participant
// is discovered.
for (size_t n = 1; n <= n_loops; ++n)
{
std::cout << "Iteration " << n << std::endl;

// Wait for undiscovery so we can wait for discovery below
EXPECT_TRUE(main_participant.wait_participant_undiscovery());

// Create another participant with authentication enabled
PubSubParticipant<HelloWorldPubSubType> other_participant(0, 0, 0, 0);
EXPECT_TRUE(other_participant.property_policy(property_policy).init_participant());

// Wait for the new participant to be discovered by the main one
EXPECT_TRUE(main_participant.wait_participant_discovery());

// The created participant gets out of scope here, and is destroyed
}

// No SECURITY error logs should have been produced
Log::Flush();
EXPECT_EQ(0u, n_logs);
}

TEST_P(Security, BuiltinAuthenticationAndCryptoPlugin_besteffort_rtps_ok)
{
PubSubReader<HelloWorldPubSubType> reader(TEST_TOPIC_NAME);
Expand Down
Loading