From dec4cec5d16edaf83a00a658f32d1e032661cebc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= <gaborjbernat@gmail.com>
Date: Fri, 9 Jan 2026 10:19:39 -0800
Subject: [PATCH] Merge pull request #3013 from gaborbernat/fix-sec
Origin: https://github.com/pypa/virtualenv/commit/dec4cec5d16edaf83a00a658f32d1e032661cebc
docs/changelog/3013.bugfix.rst | 1 +
src/virtualenv/app_data/__init__.py | 11 +++++------
src/virtualenv/util/lock.py | 7 +++----
3 files changed, 9 insertions(+), 10 deletions(-)
create mode 100644 docs/changelog/3013.bugfix.rst
new file mode 100644
@@ -0,0 +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`.
@@ -34,12 +34,11 @@ def make_app_data(folder, **kwargs):
if is_read_only:
return ReadOnlyAppData(folder)
- if not os.path.isdir(folder):
- try:
- os.makedirs(folder)
- logging.debug("created app data folder %s", folder)
- except OSError as exception:
- logging.info("could not create app data folder %s due to %r", folder, exception)
+ try:
+ os.makedirs(folder, exist_ok=True)
+ logging.debug("created app data folder %s", folder)
+ except OSError as exception:
+ logging.info("could not create app data folder %s due to %r", folder, exception)
if os.access(folder, os.W_OK):
return AppDataDiskFolder(folder)
@@ -15,9 +15,8 @@ from filelock import FileLock, Timeout
class _CountedFileLock(FileLock):
def __init__(self, lock_file) -> None:
parent = os.path.dirname(lock_file)
- if not os.path.isdir(parent):
- with suppress(OSError):
- os.makedirs(parent)
+ with suppress(OSError):
+ os.makedirs(parent, exist_ok=True)
super().__init__(lock_file)
self.count = 0
@@ -109,7 +108,7 @@ class ReentrantFileLock(PathLockBase):
# a lock, but that lock might then become expensive, and it's not clear where that lock should live.
# Instead here we just ignore if we fail to create the directory.
with suppress(OSError):
- os.makedirs(str(self.path))
+ os.makedirs(str(self.path), exist_ok=True)
try:
lock.acquire(0.0001)
--
2.50.1