From 9a075327f19f781af90a61468ab19b385b165c49 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Tue, 28 Mar 2023 16:18:24 +0200 Subject: [PATCH] Add missing mWpaSupplicantMutex locks --- src/platform/Linux/ConnectivityManagerImpl.cpp | 14 ++++++++++++++ src/platform/Linux/ConnectivityManagerImpl.h | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/platform/Linux/ConnectivityManagerImpl.cpp b/src/platform/Linux/ConnectivityManagerImpl.cpp index e3385be1bbfefc..57bba172fe3590 100644 --- a/src/platform/Linux/ConnectivityManagerImpl.cpp +++ b/src/platform/Linux/ConnectivityManagerImpl.cpp @@ -158,6 +158,7 @@ ConnectivityManager::WiFiStationMode ConnectivityManagerImpl::_GetWiFiStationMod { if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled) { + std::lock_guard lock(mWpaSupplicantMutex); mWiFiStationMode = (mWpaSupplicant.iface != nullptr) ? kWiFiStationMode_Enabled : kWiFiStationMode_Disabled; } @@ -724,6 +725,8 @@ void ConnectivityManagerImpl::_OnWpaProxyReady(GObject * source_object, GAsyncRe void ConnectivityManagerImpl::StartWiFiManagement() { + std::lock_guard lock(mWpaSupplicantMutex); + mConnectivityFlag.ClearAll(); mWpaSupplicant = GDBusWpaSupplicant{}; @@ -747,6 +750,8 @@ void ConnectivityManagerImpl::DriveAPState() CHIP_ERROR err = CHIP_NO_ERROR; WiFiAPState targetState; + std::lock_guard lock(mWpaSupplicantMutex); + // If the AP interface is not under application control... if (mWiFiAPMode != kWiFiAPMode_ApplicationControlled) { @@ -862,6 +867,8 @@ CHIP_ERROR ConnectivityManagerImpl::ConfigureWiFiAP() uint16_t discriminator = 0; char ssid[32]; + std::lock_guard lock(mWpaSupplicantMutex); + channel = ConnectivityUtils::MapChannelToFrequency(kWiFi_BAND_2_4_GHZ, CHIP_DEVICE_CONFIG_WIFI_AP_CHANNEL); if (GetCommissionableDataProvider()->GetSetupDiscriminator(discriminator) != CHIP_NO_ERROR) @@ -956,6 +963,8 @@ ConnectivityManagerImpl::ConnectWiFiNetworkAsync(ByteSpan ssid, ByteSpan credent char ssidStr[kMaxWiFiSSIDLength + 1u] = { 0 }; char keyStr[kMaxWiFiKeyLength + 1u] = { 0 }; + std::lock_guard lock(mWpaSupplicantMutex); + VerifyOrReturnError(ssid.size() <= kMaxWiFiSSIDLength, CHIP_ERROR_INVALID_ARGUMENT); VerifyOrReturnError(credentials.size() <= kMaxWiFiKeyLength, CHIP_ERROR_INVALID_ARGUMENT); VerifyOrReturnError(mWpaSupplicant.iface != nullptr, CHIP_ERROR_INCORRECT_STATE); @@ -1042,6 +1051,9 @@ void ConnectivityManagerImpl::_ConnectWiFiNetworkAsyncCallback(GObject * source_ ConnectivityManagerImpl * this_ = reinterpret_cast(user_data); std::unique_ptr attachRes; std::unique_ptr err; + + std::lock_guard lock(mWpaSupplicantMutex); + { gboolean result = wpa_fi_w1_wpa_supplicant1_interface_call_select_network_finish(mWpaSupplicant.iface, res, &MakeUniquePointerReceiver(err).Get()); @@ -1609,6 +1621,8 @@ bool ConnectivityManagerImpl::_GetBssInfo(const gchar * bssPath, NetworkCommissi void ConnectivityManagerImpl::_OnWpaInterfaceScanDone(GObject * source_object, GAsyncResult * res, gpointer user_data) { + std::lock_guard lock(mWpaSupplicantMutex); + ChipLogProgress(DeviceLayer, "wpa_supplicant: network scan done"); gchar ** bsss = wpa_fi_w1_wpa_supplicant1_interface_dup_bsss(mWpaSupplicant.iface); gchar ** oldBsss = bsss; diff --git a/src/platform/Linux/ConnectivityManagerImpl.h b/src/platform/Linux/ConnectivityManagerImpl.h index 9f98d78cd6d81d..b3a87ea1f6735b 100644 --- a/src/platform/Linux/ConnectivityManagerImpl.h +++ b/src/platform/Linux/ConnectivityManagerImpl.h @@ -207,7 +207,9 @@ class ConnectivityManagerImpl final : public ConnectivityManager, static bool mAssociationStarted; static BitFlags mConnectivityFlag; - static struct GDBusWpaSupplicant mWpaSupplicant; + static GDBusWpaSupplicant mWpaSupplicant; + // Access to mWpaSupplicant has to be protected by a mutex because it is accessed from + // the CHIP event loop thread and dedicated D-Bus thread started by platform manager. static std::mutex mWpaSupplicantMutex; NetworkCommissioning::Internal::BaseDriver::NetworkStatusChangeCallback * mpStatusChangeCallback = nullptr;