Skip to content

Commit 4873a6c

Browse files
CCF [bot]eddyashton
CCF [bot]
andauthored
[release/5.x] Cherry pick: Take lock before accessing sessions collection (#6629) (#6630)
Co-authored-by: Eddy Ashton <[email protected]>
1 parent 0cb409d commit 4873a6c

File tree

1 file changed

+50
-42
lines changed

1 file changed

+50
-42
lines changed

src/enclave/rpc_sessions.h

+50-42
Original file line numberDiff line numberDiff line change
@@ -616,15 +616,15 @@ namespace ccf
616616
disp, ::tcp::tcp_inbound, [this](const uint8_t* data, size_t size) {
617617
auto id = serialized::peek<ccf::tls::ConnID>(data, size);
618618

619-
auto search = sessions.find(id);
620-
if (search == sessions.end())
619+
auto session = find_session(id);
620+
if (session == nullptr)
621621
{
622622
LOG_DEBUG_FMT(
623623
"Ignoring tls_inbound for unknown or refused session: {}", id);
624624
return;
625625
}
626626

627-
search->second.second->handle_incoming_data({data, size});
627+
session->handle_incoming_data({data, size});
628628
});
629629

630630
DISPATCHER_SET_MESSAGE_HANDLER(
@@ -644,58 +644,66 @@ namespace ccf
644644
disp, udp::udp_inbound, [this](const uint8_t* data, size_t size) {
645645
auto id = serialized::peek<int64_t>(data, size);
646646

647-
auto search = sessions.find(id);
648-
if (search == sessions.end())
647+
std::shared_ptr<Session> session;
649648
{
650-
LOG_DEBUG_FMT(
651-
"Ignoring udp::udp_inbound for unknown or refused session: {}",
652-
id);
653-
return;
654-
}
655-
else if (!search->second.second && custom_protocol_subsystem)
656-
{
657-
LOG_DEBUG_FMT("Creating custom UDP session {}", id);
649+
std::lock_guard<ccf::pal::Mutex> guard(lock);
658650

659-
try
651+
auto search = sessions.find(id);
652+
if (search == sessions.end())
660653
{
661-
const auto& conn_id = search->first;
662-
const auto& interface_id = search->second.first;
654+
LOG_DEBUG_FMT(
655+
"Ignoring udp::udp_inbound for unknown or refused session: {}",
656+
id);
657+
return;
658+
}
659+
else if (!search->second.second && custom_protocol_subsystem)
660+
{
661+
LOG_DEBUG_FMT("Creating custom UDP session {}", id);
663662

664-
auto iit = listening_interfaces.find(interface_id);
665-
if (iit == listening_interfaces.end())
663+
try
666664
{
667-
LOG_DEBUG_FMT(
668-
"Failure to create custom protocol session because of "
669-
"unknown interface '{}', ignoring udp::udp_inbound for "
670-
"session: "
671-
"{}",
672-
interface_id,
673-
id);
665+
const auto& conn_id = search->first;
666+
const auto& interface_id = search->second.first;
667+
668+
auto iit = listening_interfaces.find(interface_id);
669+
if (iit == listening_interfaces.end())
670+
{
671+
LOG_DEBUG_FMT(
672+
"Failure to create custom protocol session because of "
673+
"unknown interface '{}', ignoring udp::udp_inbound for "
674+
"session: "
675+
"{}",
676+
interface_id,
677+
id);
678+
}
679+
680+
const auto& interface = iit->second;
681+
682+
search->second.second =
683+
custom_protocol_subsystem->create_session(
684+
interface.app_protocol, conn_id, nullptr);
685+
686+
if (!search->second.second)
687+
{
688+
LOG_DEBUG_FMT(
689+
"Failure to create custom protocol session, ignoring "
690+
"udp::udp_inbound for session: {}",
691+
id);
692+
return;
693+
}
674694
}
675-
676-
const auto& interface = iit->second;
677-
678-
search->second.second = custom_protocol_subsystem->create_session(
679-
interface.app_protocol, conn_id, nullptr);
680-
681-
if (!search->second.second)
695+
catch (const std::exception& ex)
682696
{
683697
LOG_DEBUG_FMT(
684-
"Failure to create custom protocol session, ignoring "
685-
"udp::udp_inbound for session: {}",
686-
id);
698+
"Failure to create custom protocol session: {}", ex.what());
687699
return;
688700
}
689701
}
690-
catch (const std::exception& ex)
691-
{
692-
LOG_DEBUG_FMT(
693-
"Failure to create custom protocol session: {}", ex.what());
694-
return;
695-
}
702+
703+
session = search->second.second;
696704
}
697705

698-
search->second.second->handle_incoming_data({data, size});
706+
session->handle_incoming_data({data, size});
699707
});
700708
}
701709
};

0 commit comments

Comments
 (0)