Commit 9e43acf2 authored by Georg Brandl's avatar Georg Brandl

bug #1177468: don't cache /dev/urandom file descriptor in os.urandom

parent 290f3829
...@@ -715,22 +715,18 @@ except NameError: # statvfs_result may not exist ...@@ -715,22 +715,18 @@ except NameError: # statvfs_result may not exist
pass pass
if not _exists("urandom"): if not _exists("urandom"):
_urandomfd = None
def urandom(n): def urandom(n):
"""urandom(n) -> str """urandom(n) -> str
Return a string of n random bytes suitable for cryptographic use. Return a string of n random bytes suitable for cryptographic use.
""" """
global _urandomfd try:
if _urandomfd is None: _urandomfd = open("/dev/urandom", O_RDONLY)
try: except:
_urandomfd = open("/dev/urandom", O_RDONLY)
except:
_urandomfd = NotImplementedError
if _urandomfd is NotImplementedError:
raise NotImplementedError("/dev/urandom (or equivalent) not found") raise NotImplementedError("/dev/urandom (or equivalent) not found")
bytes = "" bytes = ""
while len(bytes) < n: while len(bytes) < n:
bytes += read(_urandomfd, n - len(bytes)) bytes += read(_urandomfd, n - len(bytes))
close(_urandomfd)
return bytes return bytes
...@@ -150,6 +150,9 @@ Extension Modules ...@@ -150,6 +150,9 @@ Extension Modules
Library Library
------- -------
- Bug #1177468: Don't cache the /dev/urandom file descriptor for os.urandom,
as this can cause problems with apps closing all file descriptors.
- Bug #839151: Fix an attempt to access sys.argv in the warnings module - Bug #839151: Fix an attempt to access sys.argv in the warnings module
though this can be missing in embedded interpreters though this can be missing in embedded interpreters
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment