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

PyErr_SetFromErrno() etc should be called immediately after setting the error code #107913

Closed
serhiy-storchaka opened this issue Aug 13, 2023 · 0 comments
Assignees
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Aug 13, 2023

Functions like PyErr_SetFromErrno() rely on global variable errno (actually it is thread local, but it does not matter here). They should be called immediately after using a functions which set errno. Calling other function (like close()) can change the value of errno. Py_DECREF() and PyBuffer_Release() can execute arbitrary code, in particularly the code which changes the value of errno. Even PyMem_Free() is not safe, because it the memory allocator can be customized.

There is the same issue with SetFromWindowsErr() and friends. If pass 0 as Windows error code, it calls GetLastError() to retrieve the global value which can be changed at that time if some functions were called before SetFromWindowsErr().

Most uses in the code are correct, but there are several sites in the code where some cleanup code is inserted between function which sets the error code and function which consumes it.

Two ways to resolve this issue:

  1. Reorganize the code so that PyErr_SetFromErrno() and SetFromWindowsErr() are called immediately after function which sets the error code (not counting simple memory reads or writes). In some cases it may require duplicating the cleanup code (usually just one line).

  2. Save the error code to a local variable before executing the intermediate code and restore it after.

Linked PRs

@serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes labels Aug 13, 2023
@serhiy-storchaka serhiy-storchaka self-assigned this Aug 13, 2023
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 14, 2023
Functions like PyErr_SetFromErrno() and SetFromWindowsErr() should be
called immediately after using the C API which sets errno or the Windows
error code.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 14, 2023
Functions like PyErr_SetFromErrno() and SetFromWindowsErr() should be
called immediately after using the C API which sets errno or the Windows
error code.
serhiy-storchaka added a commit that referenced this issue Aug 26, 2023
Functions like PyErr_SetFromErrno() and SetFromWindowsErr() should be
called immediately after using the C API which sets errno or the Windows
error code.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Aug 26, 2023
…-107930)

Functions like PyErr_SetFromErrno() and SetFromWindowsErr() should be
called immediately after using the C API which sets errno or the Windows
error code.
(cherry picked from commit 2b15536)

Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 26, 2023
…ythonGH-107930)

Functions like PyErr_SetFromErrno() and SetFromWindowsErr() should be
called immediately after using the C API which sets errno or the Windows
error code..
(cherry picked from commit 2b15536)

Co-authored-by: Serhiy Storchaka <[email protected]>
Yhg1s pushed a commit that referenced this issue Aug 26, 2023
…) (#108523)

gh-107913: Fix possible losses of OSError error codes (GH-107930)

Functions like PyErr_SetFromErrno() and SetFromWindowsErr() should be
called immediately after using the C API which sets errno or the Windows
error code.
(cherry picked from commit 2b15536)

Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit that referenced this issue Aug 27, 2023
…) (GH-108524)

Functions like PyErr_SetFromErrno() and SetFromWindowsErr() should be
called immediately after using the C API which sets errno or the Windows
error code.
(cherry picked from commit 2b15536)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant