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

Avoid unresolved symbols from libcrypto when compiled with OQS_DLOPEN_OPENSSL #2043

Merged
merged 2 commits into from
Jan 15, 2025

Conversation

ueno
Copy link
Contributor

@ueno ueno commented Jan 15, 2025

When liboqs is compiled with OQS_DLOPEN_OPENSSL, the final shared library may contain unresolved symbols:

$ cmake -GNinja -DBUILD_SHARED_LIBS=ON -DOQS_USE_AES_OPENSSL=ON -DOQS_USE_AES_INSTRUCTIONS=OFF -DOQS_DIST_BUILD=ON -DOQS_USE_SHA3_OPENSSL=ON -DOQS_DLOPEN_OPENSSL=ON -DCMAKE_BUILD_TYPE=Debug -LAH ..
$ ninja
$ nm -g lib/liboqs.so.0.12.1-dev | grep '^[[:space:]]*U '
                 U __assert_fail@GLIBC_2.2.5
                 U CRYPTO_free
                 U CRYPTO_malloc
                 U dlopen@GLIBC_2.34
                 U dlsym@GLIBC_2.34

This PR fixes it in two ways: first only use OpenSSL memory functions when OQS_USE_OPENSSL=ON, and secondly wrap those symbols with OSSL_FUNC so they can be dynamically resolved.

  • Does this PR change the input/output behaviour of a cryptographic algorithm (i.e., does it change known answer test values)? (If so, a version bump will be required from x.y.z to x.(y+1).0.)
  • Does this PR change the list of algorithms available -- either adding, removing, or renaming? Does this PR otherwise change an API? (If so, PRs in fully supported downstream projects dependent on these, i.e., oqs-provider will also need to be ready for review and merge by the time this is merged.)

Otherwise, when the OQS_DLOPEN_OPENSSL is defined but OpenSSL is
used only partially, e.g., with OQS_USE_SHA3_OPENSSL=ON, there will be
some unresolved symbols in the final artifact:

```
$ cmake -GNinja -DBUILD_SHARED_LIBS=ON -DOQS_USE_AES_OPENSSL=ON -DOQS_USE_AES_INSTRUCTIONS=OFF -DOQS_DIST_BUILD=ON -DOQS_USE_SHA3_OPENSSL=ON -DOQS_DLOPEN_OPENSSL=ON -DCMAKE_BUILD_TYPE=Debug -LAH ..
$ ninja
$ nm -g lib/liboqs.so.0.12.1-dev | grep '^[[:space:]]*U '
                 U __assert_fail@GLIBC_2.2.5
                 U CRYPTO_free
                 U CRYPTO_malloc
                 U dlopen@GLIBC_2.34
                 U dlsym@GLIBC_2.34
```

Signed-off-by: Daiki Ueno <[email protected]>
@ueno ueno requested a review from dstebila as a code owner January 15, 2025 08:05
Copy link
Member

@baentsch baentsch left a comment

Choose a reason for hiding this comment

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

Thanks for this PR, @ueno . At first glance, it seems it should also take into consideration #2039. Also, CI failures seem relevant to address before definite review.

This enables those OpenSSL memory functions can be either resolved at
build time or at run-time through dlopen. Note that we use CRYPTO_*
functions instead of OPENSSL_* as the latter are defined as a macro
and cannot be dynamically resolved.

Signed-off-by: Daiki Ueno <[email protected]>
@ueno ueno force-pushed the wip/openssl-alloc branch from 2f871a1 to 185ea28 Compare January 15, 2025 08:17
@ueno
Copy link
Contributor Author

ueno commented Jan 15, 2025

@baentsch The <openssl/crypto.h> include has been moved to src/common/ossl_helpers.h, which is included when OQS_USE_OPENSSL is defined (without OPENSSL_VERSION_NUMBER check), so I believe #2038 is already addressed. I'm not sure what is the point of checking OPENSSL_VERSION_NUMBER in the first place though; liboqs requires OpenSSL 1.1.1 and OPENSSL_VERSION_NUMBER is defined there and the OPENSSL_*alloc and CRYPTO_*alloc etc. are also available and haven't changed since then.

@ueno ueno requested a review from baentsch January 15, 2025 13:10
Copy link
Member

@baentsch baentsch left a comment

Choose a reason for hiding this comment

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

Thanks for the update @ueno : This IMO indeed seems to fix #2038 now. Tagging @SWilson4 for a second review as he already looked into this and @baluduvvuri1 for checking the PR addresses the concerns raised.

Just a final question: Would you care about adding a CI job testing also a config with OQS_DLOPEN_OPENSSL to avoid this from re-occurring, @ueno ?

Copy link
Member

@SWilson4 SWilson4 left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! I agree that this should fix #2039.

@SWilson4 SWilson4 merged commit 64bceb3 into open-quantum-safe:main Jan 15, 2025
67 checks passed
@baentsch
Copy link
Member

So no CI testing this :-(

@SWilson4
Copy link
Member

So no CI testing this :-(

We do have a couple of CI jobs that test the OQS_DLOPEN_OPENSSL option. I believe they were passing because the #2039 bug ensured that the OpenSSL memory management functions were never actually used.

@ueno
Copy link
Contributor Author

ueno commented Jan 29, 2025

So no CI testing this :-(

We do have a couple of CI jobs that test the OQS_DLOPEN_OPENSSL option. I believe they were passing because the #2039 bug ensured that the OpenSSL memory management functions were never actually used.

That said, I guess it wouldn't hurt to have an extra check to ensure there are no unresolved symbols. I've filed #2058 for that; sorry for not doing that when this PR was being reviewed.

pablogf-uma pushed a commit to pablogf-uma/liboqs that referenced this pull request Jan 30, 2025
…_OPENSSL (open-quantum-safe#2043)

* Do not assume OpenSSL memory functions when libcrypto is dlopened

Otherwise, when the OQS_DLOPEN_OPENSSL is defined but OpenSSL is
used only partially, e.g., with OQS_USE_SHA3_OPENSSL=ON, there will be
some unresolved symbols in the final artifact:

```
$ cmake -GNinja -DBUILD_SHARED_LIBS=ON -DOQS_USE_AES_OPENSSL=ON -DOQS_USE_AES_INSTRUCTIONS=OFF -DOQS_DIST_BUILD=ON -DOQS_USE_SHA3_OPENSSL=ON -DOQS_DLOPEN_OPENSSL=ON -DCMAKE_BUILD_TYPE=Debug -LAH ..
$ ninja
$ nm -g lib/liboqs.so.0.12.1-dev | grep '^[[:space:]]*U '
                 U __assert_fail@GLIBC_2.2.5
                 U CRYPTO_free
                 U CRYPTO_malloc
                 U dlopen@GLIBC_2.34
                 U dlsym@GLIBC_2.34
```

Signed-off-by: Daiki Ueno <[email protected]>

* Wrap OpenSSL memory functions with OSSL_FUNC

This enables those OpenSSL memory functions can be either resolved at
build time or at run-time through dlopen. Note that we use CRYPTO_*
functions instead of OPENSSL_* as the latter are defined as a macro
and cannot be dynamically resolved.

Signed-off-by: Daiki Ueno <[email protected]>

---------

Signed-off-by: Daiki Ueno <[email protected]>
Signed-off-by: Pablo Gutiérrez <pablogf@MSI.>
pablogf-uma pushed a commit to pablogf-uma/liboqs that referenced this pull request Jan 30, 2025
…_OPENSSL (open-quantum-safe#2043)

* Do not assume OpenSSL memory functions when libcrypto is dlopened

Otherwise, when the OQS_DLOPEN_OPENSSL is defined but OpenSSL is
used only partially, e.g., with OQS_USE_SHA3_OPENSSL=ON, there will be
some unresolved symbols in the final artifact:

```
$ cmake -GNinja -DBUILD_SHARED_LIBS=ON -DOQS_USE_AES_OPENSSL=ON -DOQS_USE_AES_INSTRUCTIONS=OFF -DOQS_DIST_BUILD=ON -DOQS_USE_SHA3_OPENSSL=ON -DOQS_DLOPEN_OPENSSL=ON -DCMAKE_BUILD_TYPE=Debug -LAH ..
$ ninja
$ nm -g lib/liboqs.so.0.12.1-dev | grep '^[[:space:]]*U '
                 U __assert_fail@GLIBC_2.2.5
                 U CRYPTO_free
                 U CRYPTO_malloc
                 U dlopen@GLIBC_2.34
                 U dlsym@GLIBC_2.34
```

Signed-off-by: Daiki Ueno <[email protected]>

* Wrap OpenSSL memory functions with OSSL_FUNC

This enables those OpenSSL memory functions can be either resolved at
build time or at run-time through dlopen. Note that we use CRYPTO_*
functions instead of OPENSSL_* as the latter are defined as a macro
and cannot be dynamically resolved.

Signed-off-by: Daiki Ueno <[email protected]>

---------

Signed-off-by: Daiki Ueno <[email protected]>
Signed-off-by: Pablo Gutiérrez <pablogf@MSI.>
Signed-off-by: Pablo Gutiérrez <[email protected]>
pablogf-uma pushed a commit to pablogf-uma/liboqs that referenced this pull request Jan 30, 2025
…_OPENSSL (open-quantum-safe#2043)

* Do not assume OpenSSL memory functions when libcrypto is dlopened

Otherwise, when the OQS_DLOPEN_OPENSSL is defined but OpenSSL is
used only partially, e.g., with OQS_USE_SHA3_OPENSSL=ON, there will be
some unresolved symbols in the final artifact:

```
$ cmake -GNinja -DBUILD_SHARED_LIBS=ON -DOQS_USE_AES_OPENSSL=ON -DOQS_USE_AES_INSTRUCTIONS=OFF -DOQS_DIST_BUILD=ON -DOQS_USE_SHA3_OPENSSL=ON -DOQS_DLOPEN_OPENSSL=ON -DCMAKE_BUILD_TYPE=Debug -LAH ..
$ ninja
$ nm -g lib/liboqs.so.0.12.1-dev | grep '^[[:space:]]*U '
                 U __assert_fail@GLIBC_2.2.5
                 U CRYPTO_free
                 U CRYPTO_malloc
                 U dlopen@GLIBC_2.34
                 U dlsym@GLIBC_2.34
```

Signed-off-by: Daiki Ueno <[email protected]>

* Wrap OpenSSL memory functions with OSSL_FUNC

This enables those OpenSSL memory functions can be either resolved at
build time or at run-time through dlopen. Note that we use CRYPTO_*
functions instead of OPENSSL_* as the latter are defined as a macro
and cannot be dynamically resolved.

Signed-off-by: Daiki Ueno <[email protected]>

---------

Signed-off-by: Daiki Ueno <[email protected]>
Signed-off-by: Pablo Gutiérrez <pablogf@MSI.>
Signed-off-by: Pablo Gutiérrez <[email protected]>
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