Skip to content

Add read/write locks around WebAuthn volatile state - #14

Merged
emlun merged 8 commits into
webauthnfrom
webauthn-volatile-state-mutex
Mar 9, 2025
Merged

emlun merged 8 commits into
webauthnfrom
webauthn-volatile-state-mutex

Conversation

@emlun

@emlun emlun commented Feb 22, 2025

Copy link
Copy Markdown
Owner

@acolomb This is a meta-PR into syncthing#9175 to address syncthing#9175 (comment) and syncthing#9175 (comment). Does this address the locking concerns?

@acolomb acolomb left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks basically sensible to me, with a few questions. But I'm no expert on locking in Syncthing, so would welcome another opinion from @imsodin here.

Comment thread lib/api/api_auth_webauthn.go Resolved
Comment thread lib/api/api_auth_webauthn.go Resolved
Comment thread lib/api/api_auth_webauthn.go Resolved
Comment thread lib/api/api_auth_webauthn.go Outdated
}

// Load volatile WebAuthn state without acquiring a read lock.
func (s *webauthnService) unsafeLoadVolatileState() *WebauthnVolatileState {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@acolomb

acolomb commented Feb 24, 2025 •

Copy link
Copy Markdown

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.

@emlun

emlun commented Mar 1, 2025

Copy link
Copy Markdown
Owner Author

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!

Comment thread lib/api/api_auth_webauthn.go Resolved
@emlun

emlun commented Mar 3, 2025

Copy link
Copy Markdown
Owner Author

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.

@acolomb acolomb left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get this moving forward. Any remaining doubts can be raised in syncthing#9175.

@emlun
emlun merged commit 00d62dd into webauthn Mar 9, 2025
@emlun
emlun deleted the webauthn-volatile-state-mutex branch March 9, 2025 20:50

@imsodin imsodin left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion, thanks! Fixed in 76da6b3 and fdb5cf1.

}

credentialVolState, ok := webauthnVolState.Credentials[cred.ID]
if !ok || credentialVolState == nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +377 to +380
s := apiproto.WebauthnVolatileState{}
if s.Credentials == nil {
s.Credentials = make(map[string]*apiproto.WebauthnCredentialVolatileState, 1)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I missing something here - what's that nil check for resp. why not initialise Credentials when definiting s.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch! These two statements used to be in two separate functions. They were merged in f626966, but only naively.

Comment on lines +391 to +392
// Load volatile WebAuthn state without acquiring a read lock.
func (s *webauthnService) loadVolatileStateRLocked() *apiproto.WebauthnVolatileState {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's synchronizing miscDB access.

emlun added a commit that referenced this pull request Jun 14, 2025
emlun added a commit that referenced this pull request Jun 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants