From 03f05f217cd8f058f4371831ca89a4a32f93d511 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Tue, 1 Nov 2016 16:47:22 -0400 Subject: [PATCH] Prevent session corruption Mutating the session is not thread safe. // FREEBIE --- src/Messages/OWSMessageSender.m | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Messages/OWSMessageSender.m b/src/Messages/OWSMessageSender.m index 052d63772..f8a0db5c6 100644 --- a/src/Messages/OWSMessageSender.m +++ b/src/Messages/OWSMessageSender.m @@ -665,7 +665,10 @@ NSString *const OWSMessageSenderInvalidDeviceException = @"InvalidDeviceExceptio recipientId:identifier deviceId:[deviceNumber intValue]]; @try { - [builder processPrekeyBundle:bundle]; + // Mutating session state is not thread safe. + @synchronized(self) { + [builder processPrekeyBundle:bundle]; + } } @catch (NSException *exception) { if ([exception.name isEqualToString:UntrustedIdentityKeyException]) { @throw [NSException @@ -685,7 +688,11 @@ NSString *const OWSMessageSenderInvalidDeviceException = @"InvalidDeviceExceptio recipientId:identifier deviceId:[deviceNumber intValue]]; - id encryptedMessage = [cipher encryptMessage:[plainText paddedMessageBody]]; + // Mutating session state is not thread safe. + id encryptedMessage; + @synchronized (self) { + encryptedMessage = [cipher encryptMessage:[plainText paddedMessageBody]]; + } NSData *serializedMessage = encryptedMessage.serialized; TSWhisperMessageType messageType = [self messageTypeForCipherMessage:encryptedMessage];