mirror of
https://github.com/python/cpython.git
synced 2025-12-31 04:23:37 +00:00
[3.13] gh-143010: Prevent a TOCTOU issue by only calling open once (GH-143011) (#143079)
gh-143010: Prevent a TOCTOU issue by only calling open once (GH-143011)
RDM: per AZero13's research the 'x' option did not exist when this code was written, This
modernization can thus drop the fd trick in _create_carefully and just use open with 'x' to achieve the same goal more securely.
(cherry picked from commit a88d1b8dab)
Co-authored-by: AZero13 <gfunni234@gmail.com>
Co-authored-by: sobolevn <mail@sobolevn.me>
This commit is contained in:
parent
931c4d6258
commit
a6cfdb5e9a
2 changed files with 2 additions and 5 deletions
|
|
@ -2183,11 +2183,7 @@ def _unlock_file(f):
|
|||
|
||||
def _create_carefully(path):
|
||||
"""Create a file if it doesn't exist and open for reading and writing."""
|
||||
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0o666)
|
||||
try:
|
||||
return open(path, 'rb+')
|
||||
finally:
|
||||
os.close(fd)
|
||||
return open(path, 'xb+')
|
||||
|
||||
def _create_temporary(path):
|
||||
"""Create a temp file based on path and open for reading and writing."""
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Fixed a bug in :mod:`mailbox` where the precise timing of an external event could result in the library opening an existing file instead of a file it expected to create.
|
||||
Loading…
Add table
Add a link
Reference in a new issue