openssl: Data races: there is wrong memory order in refcount.h
- here should be
releaseinstead ofrelaxed: https://github.com/openssl/openssl/blob/6afaa3f41f5b65432b6700064b077032b9e0c625/include/internal/refcount.h#L51
Example of issue: https://github.com/ClickHouse/openssl/commit/10e427ee32cf74bc2d1e4945c57e3d518c737e12
But tsan doesn’t work with such fences, so for tsan (or always, there is almost no difference in asm) needed acq_rel and fence no needed
- here should be
acquirebecause openssl using it to comparereswith zero: https://github.com/openssl/openssl/blob/6afaa3f41f5b65432b6700064b077032b9e0c625/include/internal/refcount.h#L59
Some explanation:
- Trivial: If someone using
acquirethey also should writereleasesomewhere - Examples: boost::smart_ptr and llvm libc++
- More formal
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 40 (32 by maintainers)
Commits related to this issue
- Correct memory ordering in CRYPTO_DOWN_REF Theres a subtle bug in CRYPTO_DOWN_REF CRYPTO_DOWN_REF is implemented as an atomic_sub_fetch with relaxed ordering, followed by an acquire memory fence if ... — committed to nhorman/openssl by nhorman 2 years ago
- Fix memory ordering guarantees and TSAN errors If we had refcounted object allowing lockless writes the relaxed semantics on DOWN_REF would allow scheduling these writes after simultaneous release of... — committed to t8m/openssl by t8m 2 years ago
- Fix memory ordering guarantees and TSAN errors If we had refcounted object allowing lockless writes the relaxed semantics on DOWN_REF would allow scheduling these writes after simultaneous release of... — committed to t8m/openssl by t8m 2 years ago
- Fix memory ordering guarantees and TSAN errors If we had refcounted object allowing lockless writes the relaxed semantics on DOWN_REF would allow scheduling these writes after simultaneous release of... — committed to openssl/openssl by t8m 2 years ago
- Fix memory ordering guarantees and TSAN errors If we had refcounted object allowing lockless writes the relaxed semantics on DOWN_REF would allow scheduling these writes after simultaneous release of... — committed to openssl/openssl by t8m 2 years ago
- Fix memory ordering guarantees and TSAN errors If we had refcounted object allowing lockless writes the relaxed semantics on DOWN_REF would allow scheduling these writes after simultaneous release of... — committed to openssl/openssl by t8m 2 years ago
- Fix memory ordering guarantees and TSAN errors If we had refcounted object allowing lockless writes the relaxed semantics on DOWN_REF would allow scheduling these writes after simultaneous release of... — committed to ramenhost/openssl by t8m 2 years ago
- Fix memory ordering guarantees and TSAN errors If we had refcounted object allowing lockless writes the relaxed semantics on DOWN_REF would allow scheduling these writes after simultaneous release of... — committed to AlexanderBeyn/openssl by t8m 2 years ago
- Fix memory ordering guarantees and TSAN errors If we had refcounted object allowing lockless writes the relaxed semantics on DOWN_REF would allow scheduling these writes after simultaneous release of... — committed to Sashan/openssl by t8m 2 years ago
To summarize, I think there are 2 cases:
Case 1 has a theoretic problem and is reported by TSAN, but I see no actual problem. If there are actual other writes that could be reordered, it would be in case 2, step b, and the proper acquire/release constraints are there.
@nhorman please note the memory_order_relaxed for the atomic operation means that instructions before and after the atomic operation can be scheduled before or after the atomic operation regardless of where they were placed before.
However that DOES NOT mean that the atomic operation itself will work on inconsistent data. I.e. there cannot be any leaks due to two threads concurrently decrementing the refcount from 2 to 1. Otherwise this would NOT be an atomic operation at all.
The acquire or release memory ordering is best explained here https://davekilian.com/acquire-release.html
The current implementation does only work under the assumption that there are no writes to the object, except for the refcount itself. The original report was about EVP_PKEY objects, shared between two threads. For correct operation the EVP_PKEY objects must be fully constructed before they are shared between multiple threads, otherwise race conditions are unavoidable. So if one thread calls any of the EVP_PKEY_set functions, it will result in disaster, also a release fence will not fix this.
It is really not about the inconsistency of the refcount itself. It is about potential inconsistency of data written to the object by one thread when it is freed by another thread.
I do not think we currently have any such situation where this could realistically happen, however after reading https://davekilian.com/acquire-release.html I have to agree that at least in the theoretic sense our current use of relaxed in down ref is wrong.