Skip to content

Commit

Permalink
crypto: avoid data race in HandleOTKCounts
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Sep 16, 2024
1 parent 1e34931 commit 830136b
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions crypto/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"sync"
"sync/atomic"
"time"

"github.com/rs/zerolog"
Expand Down Expand Up @@ -67,7 +68,7 @@ type OlmMachine struct {

otkUploadLock sync.Mutex
lastOTKUpload time.Time
receivedOTKsForSelf bool
receivedOTKsForSelf atomic.Bool

CrossSigningKeys *CrossSigningKeysCache
crossSigningPubkeys *CrossSigningPublicKeysCache
Expand Down Expand Up @@ -258,16 +259,18 @@ func (mach *OlmMachine) otkCountIsForCrossSigningKey(otkCount *mautrix.OTKCount)
}

func (mach *OlmMachine) HandleOTKCounts(ctx context.Context, otkCount *mautrix.OTKCount) {
receivedOTKsForSelf := mach.receivedOTKsForSelf.Load()
if (len(otkCount.UserID) > 0 && otkCount.UserID != mach.Client.UserID) || (len(otkCount.DeviceID) > 0 && otkCount.DeviceID != mach.Client.DeviceID) {
if otkCount.UserID != mach.Client.UserID || (!mach.receivedOTKsForSelf && !mach.otkCountIsForCrossSigningKey(otkCount)) {
if otkCount.UserID != mach.Client.UserID || (!receivedOTKsForSelf && !mach.otkCountIsForCrossSigningKey(otkCount)) {
mach.Log.Warn().
Str("target_user_id", otkCount.UserID.String()).
Str("target_device_id", otkCount.DeviceID.String()).
Msg("Dropping OTK counts targeted to someone else")
}
return
} else if !receivedOTKsForSelf {
mach.receivedOTKsForSelf.Store(true)
}
mach.receivedOTKsForSelf = true

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

0 comments on commit 830136b

Please sign in to comment.