Repository navigation
Add read/write locks around WebAuthn volatile state - #14
Conversation
| } | ||
|
|
||
| // Load volatile WebAuthn state without acquiring a read lock. | ||
| func (s *webauthnService) unsafeLoadVolatileState() *WebauthnVolatileState { |
There was a problem hiding this comment.
The usual naming in Syncthing's codebase seems to be ...RLocked() if the function is expected to be called with a read lock held, ...Locked() for a full / write lock.
And in the conditional below, if the data cannot be unmarshalled, shouldn't we rather discard it? What other way is there to get rid of data that we will never be able to use again?
There was a problem hiding this comment.
The usual naming in Syncthing's codebase seems to be
...RLocked()
Thanks!
And in the conditional below, if the data cannot be unmarshalled, shouldn't we rather discard it? What other way is there to get rid of data that we will never be able to use again?
Do you mean we should proactively delete/overwrite the stored data immediately on load failure? I don't think that's necessary, it'll happen automatically on the next write to it anyway. It would also require acquiring a write lock in this function, which looks like it would be messy to handle since this function is itself called in the write function (with a write lock already acquired).
There was a problem hiding this comment.
You're right it introduces unnecessarily complex code. If there is a way to discard invalid data (on updating you say), then that's fine with me. The user might get annoyed with the message a few times, but it's already a highly unlikely scenario to begin with.
There was a problem hiding this comment.
I had a different place in mind where we do discard invalid entries while reading, that's in lib/db/observed.go. However the big difference is these are separate DB entries, while here you serialize a single blob for the current "volatile state" as a whole.
Which makes me wonder whether that's the best approach anyway? Especially with the ongoing quest to use SQLite as database backend, we could just put each credential entry to a separate record. But that's certainly no blocker for this meta-PR, just thinking out loud. We may need to revisit this as part of any DB backend rework.
|
Oh and while we're at it, I think it is very rare in Syncthing's index DB to find JSON encoded data. Shouldn't we be using protobuf serialization instead for consistency? At least the tokenmanager uses proto, which is very close to this code. |
Yeah, good point. Fixed in c0dc2bb, thanks! |
acolomb
left a comment
There was a problem hiding this comment.
Let's get this moving forward. Any remaining doubts can be raised in syncthing#9175.
imsodin
left a comment
There was a problem hiding this comment.
But I'm no expert on locking in Syncthing, so would welcome another opinion from @imsodin here.
@imsodin Do you want to review this before we merge it? No rush to do it right away, but please let us know if we should wait or not.
Pong - time~=4 months
I hope I wrote something to this effect before: Most definitely don't wait on me, especially not on a PR for a PR.
I didn't fully review resp. understand this, I only looked at the locking specifically - one inline comment there. I skimmed the rest, those comments are just minor questions/nits.
| type WebauthnCredentialVolatileState struct { | ||
| SignCount uint32 `json:"signCount,omitempty"` | ||
| LastUseTime time.Time `json:"lastUseTime,omitempty"` | ||
| volStateMut sync.RWMutex |
There was a problem hiding this comment.
While it's probably guessable what makes up volatile state in this struct, imo it's still nice for the reader to be explicit: As in if a mut isn't covering everything, using an empty line to group all the fields and the mutex protecting them together, separating them from the rest (plus a comment pointing that out).
There was a problem hiding this comment.
Oh nice, I think I was wrong on the "it's probably guassable what makes up volatile state in this struct" above. I am not updating the above despite not having sent the comment - it nicely illustrates why some hint here in the form of a comment and/or grouping is likely warranted:
The lock is applied on operations on miscDB - my guess would have been it's for the maps maps (e.g. registrationStates). So if this lock is specifically for miscDB usage, then naming it miscDBMut would imo be self-documenting enough. If there's something else this mut does, then a comment and/or grouping is imo warranted to hint at what that is.
| } | ||
|
|
||
| credentialVolState, ok := webauthnVolState.Credentials[cred.ID] | ||
| if !ok || credentialVolState == nil { |
There was a problem hiding this comment.
Dumb question in case it trigger some helpful realisation of an issue/improvement somewhere, otherwise just ignore: Why would there be a nil entry present - I'd expect whatever adds a nil entry to not do that or delete the existing entry instead.
There was a problem hiding this comment.
Yeah, there wouldn't under correct operation. But I guess I figured that since this ultimately depends on external data, the nil guard could prevent a hard crash in the (perhaps unlikely) event the DB is mildly corrupted? Or if you run different Syncthing versions on the same DB data, or something like that.
| s := apiproto.WebauthnVolatileState{} | ||
| if s.Credentials == nil { | ||
| s.Credentials = make(map[string]*apiproto.WebauthnCredentialVolatileState, 1) | ||
| } |
There was a problem hiding this comment.
Am I missing something here - what's that nil check for resp. why not initialise Credentials when definiting s.
There was a problem hiding this comment.
Ah, good catch! These two statements used to be in two separate functions. They were merged in f626966, but only naively.
| // Load volatile WebAuthn state without acquiring a read lock. | ||
| func (s *webauthnService) loadVolatileStateRLocked() *apiproto.WebauthnVolatileState { |
There was a problem hiding this comment.
Why is a read lock needed here? The only access of member fields I see is .miscDB. I assumed the locking was for the maps (e.g. registrationStates), admittedly not basing that assumption on much. Is the locking synchronising miscDB access?
There was a problem hiding this comment.
Yes, it's synchronizing miscDB access.
Per review suggestion: #14 (comment)
Per review suggestion: #14 (comment)
@acolomb This is a meta-PR into syncthing#9175 to address syncthing#9175 (comment) and syncthing#9175 (comment). Does this address the locking concerns?