envoyproxy / envoy-openssl

Envoy OpenSSL extensions
Apache License 2.0
44 stars 39 forks source link

bssl-compat: Add rand function used by Envoy #54

Closed pfl closed 1 year ago

pfl commented 1 year ago

Accessing RAND_bytes() from OpenSSL with dlsym() keeps the library in line with C calling conventions. I was trying out C++ namespaces, but that caused the source code and calling conventions become C++, which wasn't what was intended. There is also a minimal "test" program in my branch envoy_rand_dlopen_test should anyone want to print out a string of random bytes.

Currently this links to the first OpenSSL library found, which is the one installed by the system. Some work needed here if OpenSSL is to be downloaded, compiled and linked against.


Add RANDbytes() function which is the only function used from the RAND family by Envoy. Find the identically named OpenSSL function with dlsym and store its value for next calls to this function.

Watch out for return values, OpenSSL uses < 0 to indicate errors in version 1.1.1 (and 0 in 3.0) where BoringSSL always uses 0.

Signed-off-by: Patrik Flykt patrik.flykt@intel.com

twghu commented 1 year ago
pfl commented 1 year ago

If C compatibility is desired, I can't figure out any other way of doing this except with a prefixed wrapper function + preprocessor define. If I use C++ and a namespace, the whole library turns into C++ calling conventions.

tedjpoole commented 1 year ago

@pfl I concur this looks like the only reasonable way to achieve the function interception/wrapping. Also, compared to the dlopen()/dlsym() mechanism, there is no concern with thread safety or indirection overhead, because it's all screwed together at link time rather than run time.

pfl commented 1 year ago

The options so far are:

  1. dlsym(), the first approach
  2. prefix + preprocessor define as above
  3. library symbol mangling, but if openssl is to be updated/changed afterwards, this is not feasible
  4. do a C++ only library We have one vote from @tedjpoole for 2., if I read him correctly
pfl commented 1 year ago

As discussed, let's go with dlopen(). The initialization of the dlsym() accessed function pointers are not in the library constructor function specified with attribute ((constructor)). Thus we get needed functions nicely in place when the library is loaded.

pfl commented 1 year ago

Looks like this PR updates submodule envoy also, is it intentional?

That is a bug, will fix.