[issue21082] os.makedirs(exist_ok=True) is not thread-safe: umask is set temporary to 0, serious security problem

STINNER Victor report at bugs.python.org
Fri Mar 28 11:26:39 CET 2014


STINNER Victor added the comment:

> I think the behaviour that an error is raised if the permissions are not the same is a nuisance that does not correspond to actual use cases (*).

I was also surprised that makedirs() checks for the exact permission.

We can probably document that makedirs(exists_ok=True) leaves the
directory permission unchanged if the directory already exist, and
that an explicit chmod() may be needed to ensure that permissions are
the expected permissions.

If the check on permissions is removed, an enhancement would be to
return a flag to indicate if at least one directory of the path
already existed. So the caller can avoid calling chmod() if all
directories of the path had to be created.

Something like:

if makedirs("a/b", mod=0o755, exists_ok=True):
  os.chmod("a", 0o755)
  os.chmod("a/b", 0o755)
# else a and b were created with the permission 0o755

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21082>
_______________________________________


More information about the Python-bugs-list mailing list