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

Replace malloc/free with OPENSSL_malloc/OpenSSL_free #1823

Closed
songlingatpan opened this issue Jun 25, 2024 · 6 comments
Closed

Replace malloc/free with OPENSSL_malloc/OpenSSL_free #1823

songlingatpan opened this issue Jun 25, 2024 · 6 comments
Labels
help wanted Asking for support from non-core team

Comments

@songlingatpan
Copy link
Contributor

In OpenSSL code, we can customize malloc/free functions with CRYPTO_set_mem_functions.
in oqs provider, it also utilized OPENSSL_malloc/OPENSSL_free. so CRYPTO_set_mem_functions can be used to customize allocator.
We should replace malloc/free with OPENSSL_malloc/OpenSSL_free in liboqs as well.

@SWilson4
Copy link
Member

SWilson4 commented Jul 9, 2024

We certainly could do this, but I don't think it's very high on our priority list given the amount of manual effort it would require and the minimal (to my knowledge) improvement to the library. However, if you would like to take a stab at it, please feel free to open a PR for us to review.

@ajbozarth ajbozarth moved this to Todo in liboqs planning Jul 23, 2024
@baentsch baentsch added the help wanted Asking for support from non-core team label Aug 20, 2024
@songlingatpan
Copy link
Contributor Author

@baentsch @SWilson4
I can help on this. However, I have a few questions.

  1. For the malloc and free from upstream code, if we change them to OPENSSL_malloc and OPENSSL_free, how do we maintain the code? We can write a script to replace malloc and free. Would you like to run the script during merge the code from upstream, or during build the code?
  2. Would you like to use OPENSSL_malloc and OPENSSL_free only when OQS_USE_OPENSSL = 1? or we can use it by default?
  3. I found quite some error handling issues, and memory leak after failure in both oqs and upstream code. Again, how do you maintain the code change to fix upstream issues?
    Thanks

@baentsch
Copy link
Member

:> Would you like to run the script during merge the code from upstream, or during build the code?

My preference would be to do this during copy_from_upstreamsuch as for everyone to read what code gets built.

Would you like to use OPENSSL_malloc and OPENSSL_free only when OQS_USE_OPENSSL = 1? or we can use it by default?

Not by default. If OQS_USE_OPENSSL=0, the current, openssl-free build must still succeed. Therefore, I'd suggest you'd add 2 macros in step 1 and define those suitably depending on the setting of OQS_USE_OPENSSL.

I found quite some error handling issues, and memory leak after failure in both oqs and upstream code. Again, how do you maintain the code change to fix upstream issues?

That's a scary question but a good one. Our usual recommendation is to contribute any fixes you develop to the upstreams so they flow back into liboqs when we run copy_from_upstream. If this does not succeed, we can create local patches -- but those would need to be maintained (as and when the upstreams change) and someone would need to take responsibility for this maintenance: Would you be willing to take that on @songlingatpan ?

@baentsch
Copy link
Member

I found quite some error handling issues, and memory leak after failure in both oqs and upstream code.

In addition to the comment around fixing things, what would be really helpful would be if you @songlingatpan would be willing to share (contribute by PR to our CI) testing unearthing these problems so we can track (resolutions, hopefully, too :).

@SWilson4
Copy link
Member

SWilson4 commented Sep 12, 2024

To the best of my knowledge, none of the upstreams from which we pull code automatically (PQClean, pq-crystals, MAYO, and CROSS) use malloc outside common code (SHA3, AES, etc). I don't think we have to worry about copy_from_upstream overwriting changes.

songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 17, 2024
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 17, 2024
…sl allocator

Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 17, 2024
Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 17, 2024
Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 17, 2024
…KEM, and NTRUPrime

Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 17, 2024
Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 17, 2024
…sl allocator

Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 17, 2024
Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 17, 2024
Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 17, 2024
…KEM, and NTRUPrime

Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 17, 2024
Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 18, 2024
…sl allocator

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

[open-quantum-safe#1823] update memory allocator for copy_from_upstream

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

[open-quantum-safe#1823] format code

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

[open-quantum-safe#1823] Use OpenSSL Memory Allocator for BIKE, FrodoKEM, and NTRUPrime

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

[open-quantum-safe#1823] Add Comments for Doxygen

Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 21, 2024
…sl allocator

Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 21, 2024
Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 21, 2024
Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 21, 2024
…KEM, and NTRUPrime

Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 21, 2024
Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 22, 2024
…sl allocator

Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 22, 2024
Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 22, 2024
Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 22, 2024
…KEM, and NTRUPrime

Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 22, 2024
Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 23, 2024
…sl allocator

Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 23, 2024
Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 23, 2024
Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 23, 2024
…KEM, and NTRUPrime

Signed-off-by: Songling Han <shan@paloaltonetworks.com>
songlingatpan added a commit to songlingatpan/liboqs that referenced this issue Sep 23, 2024
Signed-off-by: Songling Han <shan@paloaltonetworks.com>
baentsch pushed a commit that referenced this issue Oct 19, 2024
* [#1823] replace malloc/calloc/strdup/free with openssl allocator

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

* [#1823] update memory allocator for copy_from_upstream

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

* [#1823] Use OpenSSL Memory Allocator for BIKE, FrodoKEM, and NTRUPrime

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

* [#1823] Add Comments for Doxygen

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

* include openssl/crypto.h and resolve conflict varible for ntru

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

* Add openssl version check to fix build error

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

* Fix build for OQS_DLOPEN_OPENSSL

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

* remove OQS_MEM_free

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

* Add allocator check in tests/test_code_conventions.py

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

* Add IGNORE memory-check

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

* Delect checked allocation functions

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

* Revert back p_param to p for sntrup

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

* Add allocator check for '.c', '.h', '.fragment'

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

* Add NULL for previous checked allocation

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

* Add fprintf error for abort cases

Signed-off-by: Songling Han <shan@paloaltonetworks.com>

* use OQS_EXIT_IF_NULLPTR for checked malloc cases

Signed-off-by: Songling Han <shan@paloaltonetworks.com>


---------

Signed-off-by: Songling Han <shan@paloaltonetworks.com>
@SWilson4
Copy link
Member

Completed in #1926.

@github-project-automation github-project-automation bot moved this from Todo to Done in liboqs planning Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Asking for support from non-core team
Projects
Status: Done
Development

No branches or pull requests

3 participants