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

Security Implications of #675 (Delete rand.py) #706

Closed
tbaxx opened this issue Nov 2, 2017 · 9 comments · Fixed by #708
Closed

Security Implications of #675 (Delete rand.py) #706

tbaxx opened this issue Nov 2, 2017 · 9 comments · Fixed by #708

Comments

@tbaxx
Copy link

tbaxx commented Nov 2, 2017

In #675, the rand module was removed from the codebase, and users of the module were instructed to use os.urandom instead. However, using os.urandom is not an alternative for the whole rand module, just for some functions like rand.bytes.

There are other functions in the module, such as rand.add, that are changing the PRNG state of OpenSSL. Using such functions is of particular importance for fork safety, as described here: https://wiki.openssl.org/index.php/Random_fork-safety

and here:
https://docs.python.org/2/library/ssl.html#multi-processing

Please consider reverting #675, or at least bringing back the minimum subset of the rand module that will allow pyopenssl users to deal with fork safety.

@alex
Copy link
Member

alex commented Nov 2, 2017

Hmm, we could restore just the Add function, or we could have pyOpenSSL install cryptograpy's alternate RNG which is fork-safe.

Opinions?

@tiran
Copy link

tiran commented Nov 2, 2017

I'm for option 2, that is "install osrandom engine". It provides security w/o forcing the user to mess with OpenSSL internals.

@reaperhulk
Copy link
Member

I think restoring the status quo with Add is the least invasive fix here. Since pyOpenSSL gets run in some very odd embedded contexts I'd prefer not installing the cryptography engine by default (although it will continue to be installed if you use the methods that convert to cryptography objects since that happens as a side effect of the lazy backend import).

@hynek
Copy link
Contributor

hynek commented Nov 2, 2017

Maybe we should add some blurb to it explaining why it’s important while doing so.

@tbaxx
Copy link
Author

tbaxx commented Nov 18, 2017

I came across this page, which describes a new approach for fork-safety in OpenSSL 1.1.1:
https://github.com/openssl/openssl/blob/master/doc/man3/OPENSSL_fork_prepare.pod

I guess it will take some time until 1.1.1 becomes the minimum supported version, but perhaps this is worth considering while you decide how to best tackle this issue.

@reaperhulk
Copy link
Member

For posterity: we're aware of the upcoming improvements in OpenSSL 1.1.1 re: the userland CSPRNG. cryptography itself actually ships a default random engine that sources from urandom, getentropy, getrandom, or CryptGenRandom (depending on platform and OS version) to avoid this issue as well. However, to minimize any risk of incompatibility we've chosen to not import that engine by default in pyOpenSSL. You can, however, get it by importing cryptography's backend (from cryptography.hazmat.backends import default_backend) since it is an import time side effect.

#708 restores the rand subset to allow manual entropy mixing for conscientious forkers who can't utilize the cryptography random engine. In the future when these functions are called against 1.1.1 they will be a no-op.

@tbaxx
Copy link
Author

tbaxx commented Nov 20, 2017

@reaperhulk many thanks for the pull request #708 to restore part of the rand module.

Note that the security implications I am raising here have to do with OpenSSL's internal use of its own RNG. I can't think how the import of cryptography's random engine will change that. Please clarify.

@reaperhulk
Copy link
Member

cryptography contains a C implementation of rand for the ENGINE interface within OpenSSL. During startup we register it and set it to the default random. This entirely replaces OpenSSL's internal RNG and makes it fork safe 😄 You can see the implementation here: https://github.com/pyca/cryptography/blob/master/src/_cffi_src/openssl/src/osrandom_engine.c

@alex alex closed this as completed in #708 Nov 20, 2017
@tbaxx
Copy link
Author

tbaxx commented Nov 20, 2017

Thanks for clarifying!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging a pull request may close this issue.

5 participants