crypto: Make sure that we don't hold on to a mutex guard over an await.

master
Damir Jelić 2020-08-03 14:49:33 +02:00
parent df9da7539a
commit e37229554b
2 changed files with 25 additions and 6 deletions

View File

@ -196,6 +196,12 @@ mod test {
let alice_device = Device::from_account(&alice).await; let alice_device = Device::from_account(&alice).await;
store.save_devices(&[bob_device]).await.unwrap(); store.save_devices(&[bob_device]).await.unwrap();
bob_store
.read()
.await
.save_devices(&[alice_device.clone()])
.await
.unwrap();
let machine = VerificationMachine::new(alice, Arc::new(RwLock::new(Box::new(store)))); let machine = VerificationMachine::new(alice, Arc::new(RwLock::new(Box::new(store))));
let (bob_sas, start_content) = Sas::start(bob, alice_device, bob_store); let (bob_sas, start_content) = Sas::start(bob, alice_device, bob_store);

View File

@ -153,13 +153,19 @@ impl Sas {
/// string, otherwise returns a `MacEventContent` that needs to be sent to /// string, otherwise returns a `MacEventContent` that needs to be sent to
/// the server. /// the server.
pub async fn confirm(&self) -> Result<Option<ToDeviceRequest>, CryptoStoreError> { pub async fn confirm(&self) -> Result<Option<ToDeviceRequest>, CryptoStoreError> {
let mut guard = self.inner.lock().unwrap(); let (content, done) = {
let sas: InnerSas = (*guard).clone(); let mut guard = self.inner.lock().unwrap();
let (sas, content) = sas.confirm(); let sas: InnerSas = (*guard).clone();
*guard = sas; let (sas, content) = sas.confirm();
if guard.is_done() { *guard = sas;
self.mark_device_as_verified().await?; (content, guard.is_done())
};
if done {
if !self.mark_device_as_verified().await? {
return Ok(self.cancel());
}
} }
Ok(content.map(|c| { Ok(content.map(|c| {
@ -600,6 +606,13 @@ mod test {
let bob_store: Arc<RwLock<Box<dyn CryptoStore>>> = let bob_store: Arc<RwLock<Box<dyn CryptoStore>>> =
Arc::new(RwLock::new(Box::new(MemoryStore::new()))); Arc::new(RwLock::new(Box::new(MemoryStore::new())));
bob_store
.read()
.await
.save_devices(&[alice_device.clone()])
.await
.unwrap();
let (alice, content) = Sas::start(alice, bob_device, alice_store); let (alice, content) = Sas::start(alice, bob_device, alice_store);
let event = wrap_to_device_event(alice.user_id(), content); let event = wrap_to_device_event(alice.user_id(), content);