-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
unexpected behavior of tempfile.TemporaryFile() for O_TMPFILE #96531
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
Comments
Perhaps you can provide the minimum test sample. |
If the `O_TMPFILE` is available and works `tempfile.TemporaryFile` will always open the temporary file as readable, even if the mode is write only. Fix this by masking off `O_RDWR` and setting `O_WRONLY` if "r" or "+" is not specified in the mode.
The first issue means a temporary file opened write-only can be read via the underlying file descriptor, which doesn't seem ideal, and might theoretically be a security concern if the user was passing it somewhere untrusted (e.g. to use as a write-only log channel), although that seems very unlikely. import os, tempfile
with tempfile.TemporaryFile(mode='ab') as t:
t.write(b'abc')
t.seek(0)
print(os.read(t.fileno(), 3)) # b'abc' Fixing this will break any code that relies on temporary files always being implicitly readable on systems with I think the second issue is not actually a bug, although the Linux man pages are arguably ambiguous, so this is open to interpretation. With regards
However, it also says with regards
IMO the second bit should be read as a specific exception to the general rule in the first bit, and hence the behaviour is not undefined in this case. This matches with usage I could find with a quick code search: |
|
That doesn't look like the right issue: could you double-check the number, please? |
Bug report
When
O_TMPFILE
is available:tempfile.TemporaryFile(mode='wb')
opens withO_RDWR
instead ofO_WRONLY
, theopener
callback ignoresmode
argument and always use_bin_openflags
_bin_openflags
has bothO_CREAT
andO_EXCL
set, theopener
callback removesO_CREAT
from flags but leaveO_EXCL
, this is an undefined behavior according to Linuxopen(2)
manpageYour environment
Linked PRs
The text was updated successfully, but these errors were encountered: