Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
de2f1ea1
Commit
de2f1ea1
authored
Jan 06, 2017
by
Victor Stinner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
py_getentropy() now supports ENOSYS, EPERM & EINTR
Issue #29157.
parent
2f796439
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
3 deletions
+39
-3
Python/random.c
Python/random.c
+39
-3
No files found.
Python/random.c
View file @
de2f1ea1
...
@@ -183,14 +183,31 @@ py_getrandom(void *buffer, Py_ssize_t size, int blocking, int raise)
...
@@ -183,14 +183,31 @@ py_getrandom(void *buffer, Py_ssize_t size, int blocking, int raise)
#elif defined(HAVE_GETENTROPY)
#elif defined(HAVE_GETENTROPY)
#define PY_GETENTROPY 1
#define PY_GETENTROPY 1
/* Fill buffer with size pseudo-random bytes generated by getentropy().
/* Fill buffer with size pseudo-random bytes generated by getentropy():
Return 1 on success, or raise an exception and return -1 on error.
If raise is zero, don't raise an exception on error. */
- Return 1 on success
- Return 0 if getentropy() syscall is not available (failed with ENOSYS or
EPERM).
- Raise an exception (if raise is non-zero) and return -1 on error:
if getentropy() failed with EINTR, raise is non-zero and the Python signal
handler raised an exception, or if getentropy() failed with a different
error.
getentropy() is retried if it failed with EINTR: interrupted by a signal. */
static
int
static
int
py_getentropy
(
char
*
buffer
,
Py_ssize_t
size
,
int
raise
)
py_getentropy
(
char
*
buffer
,
Py_ssize_t
size
,
int
raise
)
{
{
/* Is getentropy() supported by the running kernel? Set to 0 if
getentropy() failed with ENOSYS or EPERM. */
static
int
getentropy_works
=
1
;
if
(
!
getentropy_works
)
{
return
0
;
}
while
(
size
>
0
)
{
while
(
size
>
0
)
{
/* getentropy() is limited to returning up to 256 bytes. Call it
multiple times if more bytes are requested. */
Py_ssize_t
len
=
Py_MIN
(
size
,
256
);
Py_ssize_t
len
=
Py_MIN
(
size
,
256
);
int
res
;
int
res
;
...
@@ -204,6 +221,25 @@ py_getentropy(char *buffer, Py_ssize_t size, int raise)
...
@@ -204,6 +221,25 @@ py_getentropy(char *buffer, Py_ssize_t size, int raise)
}
}
if
(
res
<
0
)
{
if
(
res
<
0
)
{
/* ENOSYS: the syscall is not supported by the running kernel.
EPERM: the syscall is blocked by a security policy (ex: SECCOMP)
or something else. */
if
(
errno
==
ENOSYS
||
errno
==
EPERM
)
{
getentropy_works
=
0
;
return
0
;
}
if
(
errno
==
EINTR
)
{
if
(
raise
)
{
if
(
PyErr_CheckSignals
())
{
return
-
1
;
}
}
/* retry getentropy() if it was interrupted by a signal */
continue
;
}
if
(
raise
)
{
if
(
raise
)
{
PyErr_SetFromErrno
(
PyExc_OSError
);
PyErr_SetFromErrno
(
PyExc_OSError
);
}
}
...
...
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