Skip to content

Commit a6cfdb5

Browse files
miss-islingtonAZero13sobolevn
authored
[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 a88d1b8) Co-authored-by: AZero13 <gfunni234@gmail.com> Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent 931c4d6 commit a6cfdb5

File tree

2 files changed

+2
-5
lines changed

2 files changed

+2
-5
lines changed

Lib/mailbox.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,11 +2183,7 @@ def _unlock_file(f):
21832183

21842184
def _create_carefully(path):
21852185
"""Create a file if it doesn't exist and open for reading and writing."""
2186-
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0o666)
2187-
try:
2188-
return open(path, 'rb+')
2189-
finally:
2190-
os.close(fd)
2186+
return open(path, 'xb+')
21912187

21922188
def _create_temporary(path):
21932189
"""Create a temp file based on path and open for reading and writing."""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

0 commit comments

Comments
 (0)