Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
e472aeaf
Kaydet (Commit)
e472aeaf
authored
Nis 26, 2014
tarafından
Antoine Pitrou
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #21207: Detect when the os.urandom cached fd has been closed or replaced, and open it anew.
üst
86fe53e9
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
10 deletions
+85
-10
test_os.py
Lib/test/test_os.py
+43
-0
NEWS
Misc/NEWS
+3
-0
random.c
Python/random.c
+39
-10
No files found.
Lib/test/test_os.py
Dosyayı görüntüle @
e472aeaf
...
@@ -1070,6 +1070,49 @@ class URandomTests(unittest.TestCase):
...
@@ -1070,6 +1070,49 @@ class URandomTests(unittest.TestCase):
"""
"""
assert_python_ok
(
'-c'
,
code
)
assert_python_ok
(
'-c'
,
code
)
def
test_urandom_fd_closed
(
self
):
# Issue #21207: urandom() should reopen its fd to /dev/urandom if
# closed.
code
=
"""if 1:
import os
import sys
os.urandom(4)
os.closerange(3, 256)
sys.stdout.buffer.write(os.urandom(4))
"""
rc
,
out
,
err
=
assert_python_ok
(
'-Sc'
,
code
)
def
test_urandom_fd_reopened
(
self
):
# Issue #21207: urandom() should detect its fd to /dev/urandom
# changed to something else, and reopen it.
with
open
(
support
.
TESTFN
,
'wb'
)
as
f
:
f
.
write
(
b
"x"
*
256
)
self
.
addCleanup
(
os
.
unlink
,
support
.
TESTFN
)
code
=
"""if 1:
import os
import sys
os.urandom(4)
for fd in range(3, 256):
try:
os.close(fd)
except OSError:
pass
else:
# Found the urandom fd (XXX hopefully)
break
os.closerange(3, 256)
with open({TESTFN!r}, 'rb') as f:
os.dup2(f.fileno(), fd)
sys.stdout.buffer.write(os.urandom(4))
sys.stdout.buffer.write(os.urandom(4))
"""
.
format
(
TESTFN
=
support
.
TESTFN
)
rc
,
out
,
err
=
assert_python_ok
(
'-Sc'
,
code
)
self
.
assertEqual
(
len
(
out
),
8
)
self
.
assertNotEqual
(
out
[
0
:
4
],
out
[
4
:
8
])
rc
,
out2
,
err2
=
assert_python_ok
(
'-Sc'
,
code
)
self
.
assertEqual
(
len
(
out2
),
8
)
self
.
assertNotEqual
(
out2
,
out
)
@contextlib.contextmanager
@contextlib.contextmanager
def
_execvpe_mockup
(
defpath
=
None
):
def
_execvpe_mockup
(
defpath
=
None
):
...
...
Misc/NEWS
Dosyayı görüntüle @
e472aeaf
...
@@ -39,6 +39,9 @@ Core and Builtins
...
@@ -39,6 +39,9 @@ Core and Builtins
Library
Library
-------
-------
- Issue #21207: Detect when the os.urandom cached fd has been closed or
replaced, and open it anew.
- Issue #21291: subprocess'
s
Popen
.
wait
()
is
now
thread
safe
so
that
- Issue #21291: subprocess'
s
Popen
.
wait
()
is
now
thread
safe
so
that
multiple
threads
may
be
calling
wait
()
or
poll
()
on
a
Popen
instance
multiple
threads
may
be
calling
wait
()
or
poll
()
on
a
Popen
instance
at
the
same
time
without
losing
the
Popen
.
returncode
value
.
at
the
same
time
without
losing
the
Popen
.
returncode
value
.
...
...
Python/random.c
Dosyayı görüntüle @
e472aeaf
...
@@ -3,6 +3,9 @@
...
@@ -3,6 +3,9 @@
#include <windows.h>
#include <windows.h>
#else
#else
#include <fcntl.h>
#include <fcntl.h>
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#endif
#endif
#ifdef Py_DEBUG
#ifdef Py_DEBUG
...
@@ -69,7 +72,11 @@ win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
...
@@ -69,7 +72,11 @@ win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
#ifndef MS_WINDOWS
#ifndef MS_WINDOWS
static
int
urandom_fd
=
-
1
;
static
struct
{
int
fd
;
dev_t
st_dev
;
ino_t
st_ino
;
}
urandom_cache
=
{
-
1
};
/* Read size bytes from /dev/urandom into buffer.
/* Read size bytes from /dev/urandom into buffer.
Call Py_FatalError() on error. */
Call Py_FatalError() on error. */
...
@@ -109,12 +116,24 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
...
@@ -109,12 +116,24 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
{
{
int
fd
;
int
fd
;
Py_ssize_t
n
;
Py_ssize_t
n
;
struct
stat
st
;
if
(
size
<=
0
)
if
(
size
<=
0
)
return
0
;
return
0
;
if
(
urandom_fd
>=
0
)
if
(
urandom_cache
.
fd
>=
0
)
{
fd
=
urandom_fd
;
/* Does the fd point to the same thing as before? (issue #21207) */
if
(
fstat
(
urandom_cache
.
fd
,
&
st
)
||
st
.
st_dev
!=
urandom_cache
.
st_dev
||
st
.
st_ino
!=
urandom_cache
.
st_ino
)
{
/* Something changed: forget the cached fd (but don't close it,
since it probably points to something important for some
third-party code). */
urandom_cache
.
fd
=
-
1
;
}
}
if
(
urandom_cache
.
fd
>=
0
)
fd
=
urandom_cache
.
fd
;
else
{
else
{
Py_BEGIN_ALLOW_THREADS
Py_BEGIN_ALLOW_THREADS
fd
=
_Py_open
(
"/dev/urandom"
,
O_RDONLY
);
fd
=
_Py_open
(
"/dev/urandom"
,
O_RDONLY
);
...
@@ -129,14 +148,24 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
...
@@ -129,14 +148,24 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
PyErr_SetFromErrno
(
PyExc_OSError
);
PyErr_SetFromErrno
(
PyExc_OSError
);
return
-
1
;
return
-
1
;
}
}
if
(
urandom_fd
>=
0
)
{
if
(
urandom_
cache
.
fd
>=
0
)
{
/* urandom_fd was initialized by another thread while we were
/* urandom_fd was initialized by another thread while we were
not holding the GIL, keep it. */
not holding the GIL, keep it. */
close
(
fd
);
close
(
fd
);
fd
=
urandom_fd
;
fd
=
urandom_cache
.
fd
;
}
else
{
if
(
fstat
(
fd
,
&
st
))
{
PyErr_SetFromErrno
(
PyExc_OSError
);
close
(
fd
);
return
-
1
;
}
else
{
urandom_cache
.
fd
=
fd
;
urandom_cache
.
st_dev
=
st
.
st_dev
;
urandom_cache
.
st_ino
=
st
.
st_ino
;
}
}
}
else
urandom_fd
=
fd
;
}
}
Py_BEGIN_ALLOW_THREADS
Py_BEGIN_ALLOW_THREADS
...
@@ -168,9 +197,9 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
...
@@ -168,9 +197,9 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
static
void
static
void
dev_urandom_close
(
void
)
dev_urandom_close
(
void
)
{
{
if
(
urandom_fd
>=
0
)
{
if
(
urandom_
cache
.
fd
>=
0
)
{
close
(
urandom_fd
);
close
(
urandom_
cache
.
fd
);
urandom_fd
=
-
1
;
urandom_
cache
.
fd
=
-
1
;
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment