openssl: Data races: there is wrong memory order in refcount.h

  1. here should be release instead of relaxed: 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

  1. here should be acquire because openssl using it to compare res with zero: https://github.com/openssl/openssl/blob/6afaa3f41f5b65432b6700064b077032b9e0c625/include/internal/refcount.h#L59

Some explanation:

  1. Trivial: If someone using acquire they also should write release somewhere
  2. Examples: boost::smart_ptr and llvm libc++
  3. More formal

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 40 (32 by maintainers)

Commits related to this issue

Most upvoted comments

To summarize, I think there are 2 cases:

  1. Only the refcounter is changed: a. T1: decrease the refounter b. T2: decrease the refcounter (happened after a) c. T2: acquire fence (doesn’t do anything useful) d. T2: free (happens after b and c)
  2. Other members of the struct are also changed a. T1: acquire a lock b. T1: do read and/or writes c. T1: release the lock d. T1: decrease the refcounter (happens after c) e. T2: decrease the refcounter (happened after d) f. T2: acquire fence g. T2: call free (happens after c and e)

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.