Re:A Large-Scale Security-Oriented Static Analysis of Python...
by make3 on Thursday July 29, 2021 @08:57PM
(#28003320)
why is mktemp insecure?
[ Parent
| on HN ]
Re:A Large-Scale Security-Oriented Static Analysis of Python...
by goodside on Thursday July 29, 2021 @09:52PM
(#28003605)
Python mktemp has been deprecated since 2003. I there’s a race condition that allows a malicious process to slip in a file or symlink with different permissions at the path mktemp returns.
Re:A Large-Scale Security-Oriented Static Analysis of Python...
by tyingq on Friday July 30, 2021 @12:52AM
(#28004593)
Just for clarity, the race condition seems deliberate. It just returns a path and leaves it to you to create the file.
Re:A Large-Scale Security-Oriented Static Analysis of Python...
by goodside on Friday July 30, 2021 @03:32AM
(#28005377)
Yes — the core problem is you can’t be guaranteed exclusive write access to a path without actually creating a file or directory.
The warning in the docs (https://docs.python.org/3/library/tempfile.html#tempfile.mkt...) aims to show an example of how to get “just a path” by making and deleting a NamedTemporaryFile, but the example is confusing and has unnecessary steps. Seems like you could just do `with NamedTemporaryFile() as f: path = f.name`.
Also, the warning seems to imply using `NamedTemporaryFile` addresses the race condition, but it doesn’t — the problem exists even for “secure” methods any time you re-use the path after cleanup.