Skip to content

Commit 830136b

Browse files
committed
crypto: avoid data race in HandleOTKCounts
1 parent 1e34931 commit 830136b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

crypto/machine.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"fmt"
1313
"sync"
14+
"sync/atomic"
1415
"time"
1516

1617
"github.com/rs/zerolog"
@@ -67,7 +68,7 @@ type OlmMachine struct {
6768

6869
otkUploadLock sync.Mutex
6970
lastOTKUpload time.Time
70-
receivedOTKsForSelf bool
71+
receivedOTKsForSelf atomic.Bool
7172

7273
CrossSigningKeys *CrossSigningKeysCache
7374
crossSigningPubkeys *CrossSigningPublicKeysCache
@@ -258,16 +259,18 @@ func (mach *OlmMachine) otkCountIsForCrossSigningKey(otkCount *mautrix.OTKCount)
258259
}
259260

260261
func (mach *OlmMachine) HandleOTKCounts(ctx context.Context, otkCount *mautrix.OTKCount) {
262+
receivedOTKsForSelf := mach.receivedOTKsForSelf.Load()
261263
if (len(otkCount.UserID) > 0 && otkCount.UserID != mach.Client.UserID) || (len(otkCount.DeviceID) > 0 && otkCount.DeviceID != mach.Client.DeviceID) {
262-
if otkCount.UserID != mach.Client.UserID || (!mach.receivedOTKsForSelf && !mach.otkCountIsForCrossSigningKey(otkCount)) {
264+
if otkCount.UserID != mach.Client.UserID || (!receivedOTKsForSelf && !mach.otkCountIsForCrossSigningKey(otkCount)) {
263265
mach.Log.Warn().
264266
Str("target_user_id", otkCount.UserID.String()).
265267
Str("target_device_id", otkCount.DeviceID.String()).
266268
Msg("Dropping OTK counts targeted to someone else")
267269
}
268270
return
271+
} else if !receivedOTKsForSelf {
272+
mach.receivedOTKsForSelf.Store(true)
269273
}
270-
mach.receivedOTKsForSelf = true
271274

272275
minCount := mach.account.Internal.MaxNumberOfOneTimeKeys() / 2
273276
if otkCount.SignedCurve25519 < int(minCount) {

0 commit comments

Comments
 (0)