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

fix: Unsets global libcrypto rand #4424

Merged
merged 7 commits into from
Feb 23, 2024
Merged

fix: Unsets global libcrypto rand #4424

merged 7 commits into from
Feb 23, 2024

Conversation

maddeleine
Copy link
Contributor

@maddeleine maddeleine commented Feb 17, 2024

Resolved issues:

Resolves #4253

Description of changes:

Fixes issue where random is not our custom rand implementation after calling s2n_init().

Call-outs:

Testing:

Includes tests. Also, I am fixing the bug where a bunch of test output gets printed when running the s2n_random_test.c file. This seems like a really obvious fix, so maybe someone can explain to me why we never fixed this?

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@github-actions github-actions bot added the s2n-core team label Feb 17, 2024
@maddeleine maddeleine marked this pull request as ready for review February 19, 2024 23:56
@maddeleine maddeleine requested review from lrstewart, torben-hansen and goatgoose and removed request for torben-hansen February 20, 2024 00:21
tests/unit/s2n_random_test.c Show resolved Hide resolved
Comment on lines 557 to 559
RAND_set_rand_engine(NULL);
RAND_set_rand_method(NULL);

Copy link
Contributor

Choose a reason for hiding this comment

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

Why do these methods work, but how we set the engine / method below don't work? Should we be using these methods to set our randomness instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's due to how Openssl gets a random method to use when it needs random bytes. Basically there is a global variable called default_rand_meth which is used over any random engine. Normally, we are able to set our random engine as the default. However, if you call RAND_bytes before setting up our engine, that default_rand_method gets set to the Openssl randomness function, and therefore our randomness method will never be chosen over Openssl's.
To get around this we have to call a function that alters the default_rand_method variable after it's already been set. So we basically have two options, either clean up the default random method before setting up the engine code(what I did), or calling RAND_set_rand_engine() with our custom rand method. I don't think it really matters which one we choose.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another piece of the puzzle, apparently we used to use the RAND_set_rand_method() before replacing it with ENGINE code. But it's very weird that the ENGINE code doesn't fully replace the RAND code.

Copy link
Contributor

Choose a reason for hiding this comment

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

The behavior of the default_rand_meth global is very confusing. If I'm reading the OpenSSL documentation correctly, this shouldn't be happening:
https://www.openssl.org/docs/man1.0.2/man3/RAND_set_rand_method.html#notes

If a default ENGINE is specified for RAND functionality using an ENGINE API function, that will override any RAND defaults set using the RAND API (ie. RAND_set_rand_method()). For this reason, the ENGINE API is the recommended way to control default implementations for use in RAND and other cryptographic algorithms.

But other than calling RAND_set_rand_method()/RAND_set_rand_engine(), there doesn't seem to be a way to override the default_rand_meth variable after it's set, except for OPENSSL_cleanup().

Given that the RAND methods are deprecated, it seems like it might be best to use Madeleine's current implementation and keep the ENGINE APIs to actually set our random implementation.

Copy link
Contributor Author

@maddeleine maddeleine Feb 21, 2024

Choose a reason for hiding this comment

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

Both ENGINE APIs and RAND APIs are deprecated. Rather than spending time getting our ENGINE code right, I think we should apply this fix and work towards this solution in the long term: #4348. If this is a bug, I doubt Openssl would care about it since technically the new thing for this functionality is "providers".
Also a note: I don't think there is a way to do what we want with the ENGINE APIs after staring at the codepaths for a bit. Nothing touches that default_rand_meth except RAND APIs.

utils/s2n_random.c Outdated Show resolved Hide resolved
Comment on lines 557 to 559
RAND_set_rand_engine(NULL);
RAND_set_rand_method(NULL);

Copy link
Contributor

Choose a reason for hiding this comment

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

The behavior of the default_rand_meth global is very confusing. If I'm reading the OpenSSL documentation correctly, this shouldn't be happening:
https://www.openssl.org/docs/man1.0.2/man3/RAND_set_rand_method.html#notes

If a default ENGINE is specified for RAND functionality using an ENGINE API function, that will override any RAND defaults set using the RAND API (ie. RAND_set_rand_method()). For this reason, the ENGINE API is the recommended way to control default implementations for use in RAND and other cryptographic algorithms.

But other than calling RAND_set_rand_method()/RAND_set_rand_engine(), there doesn't seem to be a way to override the default_rand_meth variable after it's set, except for OPENSSL_cleanup().

Given that the RAND methods are deprecated, it seems like it might be best to use Madeleine's current implementation and keep the ENGINE APIs to actually set our random implementation.

@maddeleine maddeleine merged commit 91ad940 into main Feb 23, 2024
34 checks passed
@maddeleine maddeleine deleted the ossl_rand branch February 23, 2024 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Taking ownership of OpenSSL randomness backend doesn't always work
3 participants