[Nearby Connections] Fix UAF in OfflineServiceController during shutdown

PiperOrigin-RevId: 923343443
This commit is contained in:
Edwin Wu
2026-05-29 05:06:16 -07:00
committed by Copybara-Service
parent 01ff913277
commit d4cd7ba4be
3 changed files with 30 additions and 0 deletions
@@ -90,6 +90,7 @@ OfflineServiceController::StartListeningForIncomingConnections(
ClientProxy* client, absl::string_view service_id,
v3::ConnectionListener listener,
const v3::ConnectionListeningOptions& options) {
if (stop_) return {{Status::kOutOfOrderApiCall}, {}};
LOG(INFO) << "Client " << client->GetClientId()
<< " requested to start listening for service_id " << service_id;
return pcp_manager_.StartListeningForIncomingConnections(
@@ -98,6 +99,7 @@ OfflineServiceController::StartListeningForIncomingConnections(
void OfflineServiceController::StopListeningForIncomingConnections(
ClientProxy* client) {
if (stop_) return;
LOG(INFO) << "Client " << client->GetClientId()
<< " requested to stop listening for service_id "
<< client->GetListeningForIncomingConnectionsServiceId();
@@ -567,6 +567,33 @@ TEST_P(OfflineServiceControllerTest, ShutdownBwuManagerExecutors) {
env_.Stop();
}
TEST_P(OfflineServiceControllerTest, TestNoStartListeningAfterStop) {
env_.Start();
OfflineSimulationUser user_a(kDeviceA, GetParam());
v3::ConnectionListener listener;
v3::ConnectionListeningOptions options;
user_a.Stop();
auto result = user_a.StartListeningForIncomingConnections(
std::string(kServiceId), listener, options);
EXPECT_EQ(result.first.value, Status::kOutOfOrderApiCall);
env_.Stop();
}
TEST_P(OfflineServiceControllerTest, TestNoStopListeningAfterStop) {
env_.Start();
OfflineSimulationUser user_a(kDeviceA, GetParam());
user_a.Stop();
// Verify that calling StopListening after Stop does not crash.
user_a.StopListeningForIncomingConnections();
env_.Stop();
}
INSTANTIATE_TEST_SUITE_P(ParametrisedOfflineServiceControllerTest,
OfflineServiceControllerTest,
::testing::ValuesIn(kTestCases));
@@ -122,6 +122,7 @@ PcpManager::StartListeningForIncomingConnections(
ClientProxy* client, absl::string_view service_id,
v3::ConnectionListener listener,
const v3::ConnectionListeningOptions& options) {
if (shutdown_) return {{Status::kOutOfOrderApiCall}, {}};
if (!SetCurrentPcpHandler(options.strategy)) {
return {{Status::kError}, {}};
}