Skip to content

Commit dec4cec

Browse files
authored
Merge pull request #3013 from gaborbernat/fix-sec
1 parent 5fe5d38 commit dec4cec

3 files changed

Lines changed: 9 additions & 10 deletions

File tree

docs/changelog/3013.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix TOCTOU vulnerabilities in app_data and lock directory creation that could be exploited via symlink attacks - reported by :user:`tsigouris007`, fixed by :user:`gaborbernat`.

src/virtualenv/app_data/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ def make_app_data(folder, **kwargs):
3636
if is_read_only:
3737
return ReadOnlyAppData(folder)
3838

39-
if not os.path.isdir(folder):
40-
try:
41-
os.makedirs(folder)
42-
LOGGER.debug("created app data folder %s", folder)
43-
except OSError as exception:
44-
LOGGER.info("could not create app data folder %s due to %r", folder, exception)
39+
try:
40+
os.makedirs(folder, exist_ok=True)
41+
LOGGER.debug("created app data folder %s", folder)
42+
except OSError as exception:
43+
LOGGER.info("could not create app data folder %s due to %r", folder, exception)
4544

4645
if os.access(folder, os.W_OK):
4746
return AppDataDiskFolder(folder)

src/virtualenv/util/lock.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
class _CountedFileLock(FileLock):
1818
def __init__(self, lock_file) -> None:
1919
parent = os.path.dirname(lock_file)
20-
if not os.path.isdir(parent):
21-
with suppress(OSError):
22-
os.makedirs(parent)
20+
with suppress(OSError):
21+
os.makedirs(parent, exist_ok=True)
2322

2423
super().__init__(lock_file)
2524
self.count = 0
@@ -117,7 +116,7 @@ def _lock_file(self, lock, no_block=False): # noqa: FBT002
117116
# a lock, but that lock might then become expensive, and it's not clear where that lock should live.
118117
# Instead here we just ignore if we fail to create the directory.
119118
with suppress(OSError):
120-
os.makedirs(str(self.path))
119+
os.makedirs(str(self.path), exist_ok=True)
121120

122121
try:
123122
lock.acquire(0.0001)

0 commit comments

Comments
 (0)