Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data races in SignCSR #185

Merged
merged 1 commit into from
Mar 18, 2022
Merged

Fix data races in SignCSR #185

merged 1 commit into from
Mar 18, 2022

Conversation

korylprince
Copy link
Contributor

This addresses the data race problems mentioned #184 by protecting the depot in SignCSR with a mutex. Currently if SignCSR gets overloaded it can break the whole depot by not correctly incrementing the serial (multiple concurrent requests can get the same serial), or checking the CN database can be inconsistent (multiple concurrent requests can cause errors because of concurrent file access or possibly return bad data).

It might be that mutexes could be added to the depot itself, but this could get much more complex without getting many (or any) speed gains. SignCSR calls depot.Serial near the top, then calls depot.Put at the bottom which internally calls depot.Serial, and both depot.Serial calls should return the same serial, so the serial should be locked for the length of SignCSR anyways.

@klubi
Copy link
Contributor

klubi commented Mar 16, 2022

@jessepeterson @groob Would you mind taking a look at this PR?
Thanks!

@groob groob merged commit 6dcd663 into micromdm:main Mar 18, 2022
@jessepeterson
Copy link
Member

Sorry, I hadn't had a chance to look at this.

I really think this should be solved in the depot. Blocking the generic SignCSR service I don't think makes sense because there are depots that simply don't require it. For example:

https://github.com/jessepeterson/mysqlscepserver/blob/596b5de2ba4e1c3e4834b7a11f4e0e95a7aa6e4e/depot.go#L185-L192

As for the Bolt depot also calling Serial() — I think is a bug? Serial() is intended to generate a new unique serial number from the CA for a new certificate. The Depot should not be calling Serial for an existing cert to be written to the depot — that seems odd.

That's here:

serial, err := db.Serial()

This should be reading the serial from the supplied crt, I'd think.

@korylprince
Copy link
Contributor Author

Yeah, both the file and bolt depots get the serial again in Put. The nice thing about MySQL is you get transactions for free. For the bolt and file depots, you definitely need locking on Serial at least. I think removing the Serial calls in Put would fix the data race, or at least make it much less likely.

jessepeterson added a commit to jessepeterson/scep that referenced this pull request Dec 1, 2023
…r req. also undo locking done in micromdm#185 in lieu of this fix
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.

4 participants