From 9f009c7e6501862fbd55f8d472a16691ee6a2328 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Fri, 19 Apr 2019 15:44:11 -0400
Subject: [PATCH] jamiaccount: protect device list
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Change-Id: I6c533621bc21c01601ba2681b0ebea35f8d6cc17
Reviewed-by: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
---
 src/jamidht/jamiaccount.cpp | 14 +++++++++++++-
 src/jamidht/jamiaccount.h   |  6 ++++--
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp
index 0e627edfa1..03497bbc58 100644
--- a/src/jamidht/jamiaccount.cpp
+++ b/src/jamidht/jamiaccount.cpp
@@ -763,7 +763,11 @@ JamiAccount::createRingDevice(const dht::crypto::Identity& id)
     ringDeviceName_ = ip_utils::getDeviceName();
     if (ringDeviceName_.empty())
         ringDeviceName_ = ringDeviceId_.substr(8);
-    knownDevices_.emplace(deviceId, KnownDevice{dev_id.second, ringDeviceName_, clock::now()});
+
+    {
+        std::lock_guard<std::mutex> devicelock(deviceListMutex_);
+        knownDevices_.emplace(deviceId, KnownDevice{dev_id.second, ringDeviceName_, clock::now()});
+    }
 
     receipt_ = makeReceipt(id);
     receiptSignature_ = id.first->sign({receipt_.begin(), receipt_.end()});
@@ -1126,6 +1130,7 @@ JamiAccount::revokeDevice(const std::string& password, const std::string& device
             emitSignal<DRing::ConfigurationSignal::DeviceRevocationEnded>(getAccountID(), device, 2);
             return;
         }
+        std::lock_guard<std::mutex> lock(deviceListMutex_);
         foundAccountDevice(crt);
         AccountArchive a;
         try {
@@ -1551,6 +1556,7 @@ JamiAccount::setAccountDetails(const std::map<std::string, std::string>& details
     loadAccount(archive_password, archive_pin, archive_path);
 
     // update device name if necessary
+    std::lock_guard<std::mutex> devicelock(deviceListMutex_);
     auto dev = knownDevices_.find(dht::InfoHash(ringDeviceId_));
     if (dev != knownDevices_.end()) {
         if (dev->second.name != ringDeviceName_) {
@@ -2200,6 +2206,7 @@ JamiAccount::doRegister_()
                 dht_.put(h, crl, dht::DoneCallback{}, {}, true);
             dht_.listen<DeviceAnnouncement>(h, [this](DeviceAnnouncement&& dev) {
                 findCertificate(dev.dev, [this](const std::shared_ptr<dht::crypto::Certificate>& crt) {
+                    std::lock_guard<std::mutex> lock(deviceListMutex_);
                     foundAccountDevice(crt);
                 });
                 return true;
@@ -2277,6 +2284,7 @@ JamiAccount::doRegister_()
                         JAMI_WARN("Can't find certificate for device %s", sync.from.toString().c_str());
                         return;
                     }
+                    std::lock_guard<std::mutex> lock(deviceListMutex_);
                     if (not foundAccountDevice(cert))
                         return;
                     onReceiveDeviceSync(std::move(sync));
@@ -2319,6 +2327,7 @@ JamiAccount::doRegister_()
 
         dhtPeerConnector_->onDhtConnected(ringDeviceId_);
 
+        std::lock_guard<std::mutex> lock(buddyInfoMtx);
         for (auto& buddy : trackedBuddies_) {
             buddy.second.devices_cnt = 0;
             trackPresence(buddy.first, buddy.second);
@@ -2745,6 +2754,7 @@ JamiAccount::loadKnownDevices()
         return;
     }
 
+    std::lock_guard<std::mutex> lock(deviceListMutex_);
     for (const auto& d : knownDevices) {
         JAMI_DBG("[Account %s] loading known account device %s %s", getAccountID().c_str(),
                                                                     d.second.first.c_str(),
@@ -3266,6 +3276,7 @@ JamiAccount::syncDevices()
         }
     }
 
+    std::lock_guard<std::mutex> lock(deviceListMutex_);
     for (const auto& dev : knownDevices_) {
         if (dev.first.toString() == ringDeviceId_)
             sync_data.devices_known.emplace(dev.first, ringDeviceName_);
@@ -3302,6 +3313,7 @@ JamiAccount::onReceiveDeviceSync(DeviceSync&& sync)
         findCertificate(d.first, [this,d](const std::shared_ptr<dht::crypto::Certificate>& crt) {
             if (not crt)
                 return;
+            std::lock_guard<std::mutex> lock(deviceListMutex_);
             foundAccountDevice(crt, d.second);
         });
     }
diff --git a/src/jamidht/jamiaccount.h b/src/jamidht/jamiaccount.h
index 47efc8a8fe..3eb7868ff0 100644
--- a/src/jamidht/jamiaccount.h
+++ b/src/jamidht/jamiaccount.h
@@ -503,6 +503,8 @@ class JamiAccount : public SIPAccountBase {
         bool handlePendingCallList();
         bool handlePendingCall(PendingCall& pc, bool incoming);
 
+        mutable std::mutex callsMutex_ {};
+
         /**
          * DHT calls waiting for ICE negotiation
          */
@@ -513,7 +515,6 @@ class JamiAccount : public SIPAccountBase {
          */
         std::list<PendingCall> pendingSipCalls_;
         std::set<dht::Value::Id> treatedCalls_ {};
-        mutable std::mutex callsMutex_ {};
 
         mutable std::mutex messageMutex_ {};
         std::map<dht::Value::Id, PendingMessage> sentMessages_;
@@ -553,10 +554,11 @@ class JamiAccount : public SIPAccountBase {
         std::shared_ptr<dht::Value> announce_;
 
         /* this ring account associated devices */
+        mutable std::mutex deviceListMutex_ {};
         std::map<dht::InfoHash, KnownDevice> knownDevices_;
 
         /* tracked buddies presence */
-        std::mutex buddyInfoMtx;
+        mutable std::mutex buddyInfoMtx;
         std::map<dht::InfoHash, BuddyInfo> trackedBuddies_;
 
         void loadAccount(const std::string& archive_password = {}, const std::string& archive_pin = {}, const std::string& archive_path = {});
-- 
GitLab